【正文】
b)|(c(d*)) PLLab, NTHU,Cs2403 Programming Languages 19 Pattern Matching Primitives Metacharacter Matches . any character except newline \n newline * zero or more copies of the preceding expression + one or more copies of the preceding expression ? zero or one copy of the preceding expression ^ beginning of line / plement $ end of line a|b a or b (ab)+ one or more copies of ab (grouping) [ab] a or b a{3} 3 instances of a “a+b” literal “a+b” (Cescapesstill work) PLLab, NTHU,Cs2403 Programming Languages 20 Recall: Lex Source ? Lex source is a table of – regular expressions and – corresponding program fragments (actions) … %% regexp action regexp action … %% %% “=“ printf(“operator: ASSIGNMENT”)。 \n printf(“new line\n”)。 will ignore the input (no actions) [ \t\n] 。 – “ “ | “\t” | “\n” 。 %% … PLLab, NTHU,Cs2403 Programming Languages 24 Lex Predefined Variables ? yytext a string containing the lexeme ? yyleng the length of the lexeme ? yyin the input stream pointer – the default input of default main() is stdin ? yyout the output stream pointer – the default output of default main() is stdout. ? cs20: %./ inputfile outfile ? . [az]+ printf(“%s”, yytext)。 counter++。expression: expression 39。%%int yyerror(char *s){ fprintf(stderr, %s\n, s)。*39。+39。)39。*39。 { $$ = $2。*39。 { $$ = $2。*39。 { $$ = $2。*39。 { $$ = $2。 Yacc ? Simple calculator a = 4 + 6 a a=10 b = 7 c = a + b c c = 17 $ PLLab, NTHU,Cs2403 Programming Languages 52 Grammar expression ::= expression 39。 factor | factor factor ::= 39。 | statement_list statement 39。 expression: expression 39。*39。(39。 } | NAME { $$ = $1value。 /* end of input */ } \n |”=“|”+”|””|”*”|”/” return yytext[0]。 e x p r | 39。39。 highest precedence PLLab, NTHU,Cs2403 Programming Languages 60 Precedence / Association expr : expr ?+? expr { $$ = $1 + $3。 39。s start symbol `%union39。 Doug Brown – O?Reilly – ISBN: 1565920007 ? Mastering Regular Expressions – by Jeffrey . Friedl – O?Reilly – ISBN: 1565922573 。 Declare a terminal symbol (token type name) with no precedence or associativity specified `%type39。 %left 39。 } | expr ?*? expr { $$ = $1 * $3。+39。 e x p r 39。T) – yacc –d ? Bison (GNU) – bison –d –y 產(chǎn)生 , 與 yacc相同 不然會(huì)產(chǎn)生 PLLab, NTHU,Cs2403 Programming Languages 58 Precedence / Association 1. 123 = (12)3? or 1(23)? Define ?? operator is leftassociation. 2. 12*3 = 1(2*3) Define “*” operator is precedent to “” operator e x p r : e x p r 39。 %% Parser (cont’d) PLLab, NTHU,Cs2403 Programming Languages 55 %{ include include include %} %% ([09]+|([09]*\.[09]+)([eE][+]?[09]+)?) { = atof(yytext)。)39。 } | term 39。 term { $$ = $1 + $3。 。 expression 39。 term | expression 39。 $3 Default: $$ = $1。 } | factor { $$ = $1。 $2 PLLab, NTHU,Cs2403 Programming Languages 46 The Position of Rules expr : expr 39。 } | factor { $$ = $1。 $1 PLLab, NTHU,Cs2403 Programming Languages 45 The Position of Rules expr : expr 39。 } | factor { $$ = $1。 PLLab, NTHU,Cs2403 Programming Languages 44 The Position of Rules expr : expr 39。 } | factor { $$ = $1。 PLLab, NTHU,Cs2403 Programming Languages 43 The Position of Rules expr : expr 39。 term : term 39。 factor : 39。}int main(void){ yyparse()。 NUMBER { $$ = $1 + $3。 printf(“There are total %d words\n”, counter)。 [azAZ]+ {words++。 REJECT。 d = b * c。 } PLLab, NTHU,Cs2403 Programming