【正文】
ype cc –ll ? To run the lexical analyzer program, type ./ inputfile PLLab, NTHU,Cs2403 Programming Languages 30 Versions of Lex ? ATamp。 return 0。*39。 } 。 } 。 } 。 } 。39。 statement: NAME 39。/39。 return NUMBER。 ) 39。 } | expr ?/? expr { if($3==0) yyerror(“divide 0”)。 Declare the type of semantic values for a nonterminal symbol PLLab, NTHU,Cs2403 Programming Languages 63 YACC Declaration Summary `%right39。 Specify the grammar39。/39。 39。 } | NUMBER { $$ = $1。 Parser (cont’d) PLLab, NTHU,Cs2403 Programming Languages 54 term: term 39。\n39。} %{ include include %} %token CHAR, FLOAT, ID, INT %% yacc d Produced : define CHAR 258 define FLOAT 259 define ID 260 define INT 261 PLLab, NTHU,Cs2403 Programming Languages 50 YACC ? Rules may be recursive ? Rules may be ambiguous* ? Uses bottom up Shift/Reduce parsing – Get a token – Push onto stack – Can it reduced (How do we know?) ? If yes: Reduce using a rule ? If no: Get another token ? Yacc cannot look ahead more than one token Phrase cart_animal AND CART | work_animal AND PLOW … PLLab, NTHU,Cs2403 Programming Languages 51 Yacc Example ? Taken from Lex amp。 term : term 39。 term : term 39。 term : term 39。 term : term 39。 PLLab, NTHU,Cs2403 Programming Languages 42 Rules Section ? Normally written like this ? Example: expr : expr 39。 } 。 %} letter [azAZ] %% {letter}+ {printf(“a word\n”)。 ? | indicates that the action for this rule is from the action for the next rule – [ \t\n] 。 ID ASSIGN ID PLUS ID MULT ID SEMI ? Lex is an utility to help you rapidly generate your scanners PLLab, NTHU,Cs2403 Programming Languages 5 Lex – Lexical Analyzer ? Lexical analyzers tokenize input streams ? Tokens are the terminals of a language – English ? words, punctuation marks, … – Programming language ? Identifiers, operators, keywords, … ? Regular expressions define terminals/tokens PLLab, NTHU,Cs2403 Programming Languages 6 Lex Source Program ? Lex source is a table of – regular expressions and – corresponding program fragments digit [09] letter [azAZ] %% {letter}({letter}|{digit})* printf(“id: %s\n”, yytext)。d=b*c。} PLLab, NTHU,Cs2403 Programming Languages 25 Lex Library Routines ? yylex() – The default main() contains a call of yylex() ? yymore() – return the next token ? yyless(n) – retain the first n characters in yytext ? yywarp() – is called whenever Lex reaches an endoffile – The default yywarp() always returns 1 PLLab, NTHU,Cs2403 Programming Languages 26 Review of Lex Predefined Variables Name Function char *yytext pointer to matched string int yyleng length of matched string FILE *yyin input stream pointer FILE *yyout output stream pointer int yylex(void) call to invoke lexer, returns token char* yymore(void) return the next token int yyless(int n) retain the first n characters in yytext int yywrap(void) wrapup, return 1 if done, 0 if not done ECHO write matched string REJECT go to the next alternative rule INITAL initial start condition BEGIN condition switch start condition PLLab, NTHU,Cs2403 Programming Languages 27 User Subroutines Section ? You can use your Lex routines in the same ways you use routines in other programming languages. %{ void foo()。39。 expr 39。 term { $$ = $1 + $3。 term { $$ = $1 + $3。 term { $$ = $1 + $3。 term { $$ = $1 + $3。 } char { return CHAR。 | 39。39。 } | 39。 e x p r | e x p r 39。? %left 39。 39。 Declare a terminal symbol (token type name) that is nonassociative