Presentation is loading. Please wait.

Presentation is loading. Please wait.

LEX Input file: -31 4 54 6 -4 -665 Output file: -31 +4 +54 +6 -4 -665 Total=-636 %{ int total = 0; %} % [0-9]+ {printf("+"); REJECT;} [+/-]?[0-9]+ {ECHO;

Similar presentations


Presentation on theme: "LEX Input file: -31 4 54 6 -4 -665 Output file: -31 +4 +54 +6 -4 -665 Total=-636 %{ int total = 0; %} % [0-9]+ {printf("+"); REJECT;} [+/-]?[0-9]+ {ECHO;"— Presentation transcript:

1

2 LEX Input file: -31 4 54 6 -4 -665 Output file: -31 +4 +54 +6 -4 -665 Total=-636 %{ int total = 0; %} % [0-9]+ {printf("+"); REJECT;} [+/-]?[0-9]+ {ECHO; total+=atoi(yytext);} [\ \t\n] {ECHO;} % int yywrap(void) { printf(”Total=%d”, total); return 1; }

3 LEX – BEGIN MACRO & TWO STATES Answer the question (Y/N): Is the input character sequence properly created with using language described by regular expression a n b n, where n>=0 Input file: aaabbb aabbb Output file: aaabbb Y Y aabbb N %{ int a; int b; int OK=1; %} %start aaa %start bbb % ^a{a=1; BEGIN aaa; ECHO;} a{a++; ECHO;} b{b=1; BEGIN bbb; ECHO;} b{b++; ECHO;} \n{if (OK && a==b) printf(" Y\n"); else printf(" N\n"); a=0; b=0; OK=1;}.{OK=0; ECHO;}

4 LEX & YACC Our task is to compute total sum of only odd digits from input Input file: 017891 Output file: Total: 18 LEX code: %{ #include "ytab.h" %} % [02468]{yylval=atoi(yytext);return e;} [13579]{yylval=atoi(yytext);return o;} \n;.{YY_FATAL("Unexpected character!!!");} YACC code: %{ int tot = 0; %} %token o e %start X % X: A {printf("Total: %d\n", tot);} | A error{yyerror();} ; A: A o{tot += yylval;} | A e | ; % int yyerror() { printf("Syntax Error!!!\n"); exit(-1); }


Download ppt "LEX Input file: -31 4 54 6 -4 -665 Output file: -31 +4 +54 +6 -4 -665 Total=-636 %{ int total = 0; %} % [0-9]+ {printf("+"); REJECT;} [+/-]?[0-9]+ {ECHO;"

Similar presentations


Ads by Google