正在加载图片...
Transition Rules Tokens and Actions ERE <one or more blanks>{program statement ·Example: program -(real) return FLOAT: statement -begin retum BEGIN; A null statement:will ignore the input -(newline)linecount++; Four special options: I ECHO:REJECT:BEGIN: -(integer){ printf("I found an integerin"): The unmatched token is using a default action ·return INTEGER: that ECHO from the input to the output ·} indicates that the action for this rule is from the action for the next rule Ambiguous Source Rules Multiple States lex allows the user to explicitly declare If 2 rules match the same pattern,Lex will multiple states in Definitions section use the first rule. %s COMMENT Lex always chooses the longest matching Default states is INITIAL or 0 substring for its tokens. Transition rules can be classified into To overide the choice,use action REJECT different states,which will be match ex: she (s++;REJECT;} depend on states he (h++;REJECT;} BEGIN is used to change state In; <INITIAL>. ECHO: <INITIAL>"/" {BEGIN COMMENT:) <COMMENT>. {:} <COMMENT>/ (BEGIN INITIAL:) Lex Special Variables Lex library function calls identifiers used by Lex and Yacc begin with yylex( yy default main()contains a return yylex(: -yytext--a string containing the lexeme ·yywarp0 yyleng--the length of the lexeme -called by lexical analyzer if end of the input file -yyin-the input stream pointer -default yywarp()always retum 1 ·Example: ·yyless(n) -(integer){ printf("I found an integerin"): -n characters in yytext are retained sscanf(yytext"%d".&yylval): ·yymore0 return INTEGER: the next input expression recognized is to be tacked on to the end of this input -C++Comments-∥.. 55 Transition Rules • ERE <one or more blanks> { program statement program statement } • A null statement ; will ignore the input • Four special options: | ECHO; REJECT; BEGIN; • The unmatched token is using a default action that ECHO from the input to the output • | indicates that the action for this rule is from the action for the next rule Tokens and Actions • Example: – {real} return FLOAT; – begin return BEGIN; – {newline} linecount++; – {integer} { • printf("I found an integer\n"); • return INTEGER; • } Ambiguous Source Rules • If 2 rules match the same pattern, Lex will use the first rule. • Lex always chooses the longest matching substring for its tokens. • To overide the choice, use action REJECT ex: she {s++; REJECT;} he {h++; REJECT;} . | \n ; Multiple States • lex allows the user to explicitly declare multiple states ( in Definitions section ) %s COMMENT • Default states is INITIAL or 0 • Transition rules can be classified into different states, which will be match depend on states • BEGIN is used to change state Lex Special Variables • identifiers used by Lex and Yacc begin with yy – yytext -- a string containing the lexeme – yyleng -- the length of the lexeme – yyin – the input stream pointer • Example: – {integer} { • printf("I found an integer\n"); • sscanf(yytext,"%d", &yylval); • return INTEGER; • } – C++ Comments -- // ..... • //.* ; Lex library function calls • yylex() – default main() contains a return yylex(); • yywarp() – called by lexical analyzer if end of the input file – default yywarp() always return 1 • yyless(n) – n characters in yytext are retained • yymore() – the next input expression recognized is to be tacked on to the end of this input
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有