1 2 3 4 5 6 7 8 9 10 11 | void main() { yylex(); /* start the analysis*/ printf(" No of words: %d\n", wordCount); } int yywrap() { return 1; } |
1 | $ lex <file name.lex> |
1 2 | $ yacc <options> <filename ending with .y> |
1 2 3 4 5 6 7 8 | % #typedef char* string; /* to specify token types as char* */ #define YYSTYPE string /* a Yacc variable which has the value of returned token */ %} %token NAME EQ AGE %% |
1 2 3 | result: components { /* action to be taken in C */ } ; |
1 2 3 4 5 | param : NAME EQ NAME { printf("\tName:%s\tValue(name):%s\n", $1,$3);} | NAME EQ VALUE{ printf("\tName:%s\tValue(value):%s\n",$1,$3);} ; |
1 2 3 4 5 | char [A-Za-z] name {char}+ %% {name} { yylval = strdup(yytext); return NAME; } |
1 2 3 4 5 6 7 | file : record file | record ; record: NAME EQ AGE { printf("%s is now %s years old!!!", $1, $3);} ; %% |
1 2 3 4 5 | int yyerror(char* msg) { printf("Error: %s encountered at line number:%d\n", msg, yylineno); } |
1 2 3 4 5 6 7 8 | void main() { yyparse(); } int yyerror(char* msg) { printf("Error: %s encountered \n", msg); |
1 | $ yacc _d <filename.y> |
1 2 | $ cc <source file names> -ly |
1 2 | {pattern} { /* do smthg*/ return TOKEN_NAME; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |