正在加载图片...
Default Rules and Actions Default Input and Output The first and second part must exist,but If you don't write your own main()to deal may be empty,the third part and the with the input and the output of yylex(),the second %are optional. default input of default main()is stdin and If the third part dose not contain a main(),- the default output of default main()is ll will link a default main()which calls stdout. yylex()then exits. Unmatched patterns will perform a default -stdin usually is to be keyboard input stdout usually is to be screen output action,which consists of copying the input -cs20:%./a.out inputfile>outputfile to the output Some Simple Lex Source A General Lex Source Example Examples A minimum lex program: %% It only copies the input to the output unchanged. Example lex source file A trivial program to deletes three spacing This first section contains necessary characters: *c declarations and includes %% *to use throughout the lex specifications. [\tin]: +/ Another trivial example: #include <stdio.h> %% [t]+S: bin digit【o1] It deletes from the input all blanks or tabs at the ends of lines. (bin digit){ 各名 /*match all strings of 0's and 1's/ /, /Print out message with matching Now this is where you want your main *text program */ printf("BINARY:&s\n",yytext) int main(int argc,char "argv[])( / ([ab]*aa[ab]*bb[ab]*)I([ab]*bb[ab]*aa [ab]*){ call yylex to use the generated lexer /match all strings over */ (a,b)containing aa and bb yylex(); */ /* printf("AABB\n■): make sure everything was printed */ \n /ignore newlines fflush(yyout); exit(0); 33 Default Rules and Actions • The first and second part must exist, but may be empty, the third part and the second %% are optional. • If the third part dose not contain a main(), - ll will link a default main() which calls yylex() then exits. • Unmatched patterns will perform a default action, which consists of copying the input to the output Default Input and Output • If you don’t write your own main() to deal with the input and the output of yylex(), the default input of default main() is stdin and the default output of default main() is stdout. – stdin usually is to be keyboard input stdout usually is to be screen output – cs20: %./a.out < inputfile > outputfile Some Simple Lex Source Examples • A minimum lex program: %% It only copies the input to the output unchanged. • A trivial program to deletes three spacing characters: %% [ \t\n]; • Another trivial example: %% [ \t]+$; It deletes from the input all blanks or tabs at the ends of lines. A General Lex Source Example %{ /* * Example lex source file * This first section contains necessary * C declarations and includes * to use throughout the lex specifications. */ #include <stdio.h> %} bin_digit [01] %% {bin_digit}* { /* match all strings of 0's and 1's */ /* Print out message with matching * text */ printf("BINARY: %s\n", yytext); } ([ab]*aa[ab]*bb[ab]*)|([ab]*bb[ab]*aa[ab]*) { /* match all strings over * (a,b) containing aa and bb */ printf("AABB\n"); } \n ; /* ignore newlines */ %% /* * Now this is where you want your main program */ int main(int argc, char *argv[]) { /* * call yylex to use the generated lexer */ yylex(); /* * make sure everything was printed */ fflush(yyout); exit(0); }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有