1 / 11

Lex

Lex. Compiling Sequence. Lex. letter(letter|digit)* start: goto state0 state0: read c if c = letter goto state1 goto state0 state1: read c if c = letter goto state1 if c = digit goto state1 goto state2 state2: accept string. Lex Structure.

eilis
Download Presentation

Lex

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lex

  2. Compiling Sequence

  3. Lex • letter(letter|digit)* start: goto state0 state0: read c if c = letter goto state1 goto state0 state1: read c if c = letter goto state1 if c = digit goto state1 goto state2 state2: accept string

  4. Lex Structure … definitions … %% … rules … %% … subroutines … • example %% /* match everything except newline */ . ECHO; /* match newline */ \n ECHO; %% int yywrap(void) { return 1; } int main(void) { yylex(); return 0; }

  5. Example %{ int yylineno; %} %% ^(.*)\n printf("%4d\t%s", ++yylineno, yytext); %% int main(int argc, char *argv[]) { yyin = fopen(argv[1], "r"); yylex(); fclose(yyin); } gdueck@ash lex]$ lex lino.l [gdueck@ash lex]$ ls -l total 40 -rw-r--r-- 1 gdueck fcsprofs 36053 Apr 9 10:15 lex.yy.c -rw-r--r-- 1 gdueck fcsprofs 215 Apr 9 10:13 lino.l [gdueck@ash lex]$ cc lex.yy.c [gdueck@ash lex]$ ls -l total 64 -rwxr-xr-x 1 gdueck fcsprofs 20556 Apr 9 10:16 a.out -rw-r--r-- 1 gdueck fcsprofs 36053 Apr 9 10:15 lex.yy.c -rw-r--r-- 1 gdueck fcsprofs 215 Apr 9 10:13 lino.l

  6. Count Identifiers digit [0-9] letter [A-Za-z] %{ int count; %} %% /* match identifier */ {letter}({letter}|{digit})* count++; . \n %% int yywrap(void) { return 1; } int main(void) { yylex(); printf("number of identifiers = %d\n", count); return 0; }

  7. quoted strings %{ char *yylval; #include <string.h> %} %% \"[^"\n]*["\n] { yylval = strdup(yytext+1); if (yylval[yyleng-2] != '"') warning("improperly terminated string"); else yylval[yyleng-2] = 0; printf("found '%s'\n", yylval); }

  8. %{ char buf[100]; char *s; %} %x STRING %% \" { BEGIN STRING; s = buf; } <STRING>\\n { *s++ = '\n'; } <STRING>\\t { *s++ = '\t'; } <STRING>\\\" { *s++ = '\"'; } <STRING>\" { *s = 0; BEGIN 0; printf("found '%s'\n", buf); } <STRING>\n { printf("invalid string"); exit(1); } <STRING>. { *s++ = *yytext; }

  9. Yacc yacc -d bas.y # create y.tab.h, y.tab.c lex bas.l # create lex.yy.c cc lex.yy.c y.tab.c -obas.exe # compile/link

  10. calc.y %token INTEGER %% program: program expr '\n' { printf("%d\n", $2); } | ; expr: INTEGER { $$ = $1; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } ; %% int yyerror(char *s) { fprintf(stderr, "%s\n", s); return 0; } int main(void) { yyparse(); return 0; }

  11. calc.l %{ #include "y.tab.h" %} %% [0-9]+ { yylval = atoi(yytext); return INTEGER; } [-+\n] return *yytext; [ \t] ; /* skip whitespace */ . yyerror("invalid character"); %% int yywrap(void) { return 1; }

More Related