1、 50 LALR(1) : YACC CUP 230027 LALR (1) YA C C Java CUP YA C C YA C C CUP YA C C CUP YA C C CUP LALR(1) YACC; CUP Reengineering LALR(1) Parsers: YACC and CUP Anatomizing ZHANG Yu, ZHANG Lei Department of Computer Science and Technology, University of Science Parser; Yet another compiler-compiler (YAC
2、C); Constructor of useful parsers (CUP); Ambiguity; Conflict Computer Engineering 31 15 Vol.31 15 2005 8 August 2005 1000 3428(2005)15 0050 03 A TP311.51 Java JVM Java YA C C 0 C Java YA C C Java CUP 0 YA C C (.y ) LALR(1) Perl CUP perly.y CUP perly.cup Perl (1) YA C C CUP YA C C CUP / (sr ) sr / (r
3、r ) (2) Perl LALR(1) (3) YACC CUP Perl YA C C CUP 1 C YACC YA C C Bell 20 70 AT PL _e xpe c t = X ST A T E ; ( 12) CParser line 12 PL_expect YParser 12 PL_expect line PL_expect CParser 2 package package package : PACKAGE WORD ;package($2); ( 62) package( ) CParser package example; sub show “sub show
4、” “package example;” “show” 3 block block : remember lineseq if (PL_copline (line_t)$1) PL_copline = $1; $ = block_end($2, $3); ; ( 3) remember: $ = block_start(TRUE); ; ( 4) 4 block_start( ) 3 block_end( ) CParser 3 4 4 CParser sub try ($) try 1, 13 % 4 = 1; CParser 2 “try” subrout:SUB startsub sub
5、name proto subbody newSUB($2, $3, $4, $5); ; ( 53) newSUB( ) try CParser YParser sr 5 sr CUP YA C C Perl sr 114 123 te rm : sc a la r % pre c ( $ = $1; ( 114) | scalar expr ; %prec ( ( 123) “$XXX123 = 123; ” $XXX scalar YParser 123 “$XXX123” term CParser 114 YParser CParser scalar ( term 4 BYACC CUP
6、 BYACC CUP $start $startstartsymbol CUP “NT$” RHS RHS CUP lalr_state BYACC struct core BYACC CUP CUP parse_action_table parse_reduce_table LALR 0 BYacc struct shifts struct reductions BYACC CUP BYACC (1)CUP LALR BYACC .y % % % % .cup package import 52 (2)CUP lr0 BYACC lr0 (3) lr0 CUP BYACC (4)CUP BY
7、ACC sr lookahead BYACC CUP 5 YParser CParser BYACC lookahead BYACC (1) (2) CUP BYACC -compact_red 5 CUP 1 AX 1X 2X n-1X n act X n act act seg seg act RHS CUP (1) AX 1X 2X n-1N newX n act (2) N new seg N new act act seg 1 2 AX 1X 2X n-1X n act X n act act X n CUP (1) AX 1X 2X n-1act X n (2) AA bodyX
8、nA body X 1X 2X n-1act (1) 2 3 AX 1X 2X n-1X n act X n X n+ Y (“+ ” Y ) act X n X n Y CUP AA bodyA lasttoken A body X 1X 2X n-1X nbodyactX nbody* (“ *” 0 )A lasttoken Y 4 4 AX 1X 2 X 1 X 2 act act X 1 CParser YParser CUP parser code act 3 block_start( ) PL_hints warn die warn die PL_hints PL_hints b
9、lock_start() perly.cup parser code protected restoreHints() PL_hints block_start() parser.restoreHints() 5 A B %prec t %prec t t A B I i lookahead sr lookahead CParser B %prec t B C %prec t C YA C C CUP sr sr B %prec t C C 6 Perl JVM CParser YParser Perl Java 1 Johnson S C. YACCYet Another Compiler-compiler. Technical Report CS-32, AT&T Bell Laboratories, Murray Hill, NJ, 1975 2 Hudson S E. CUP Parser Generator for Java (v0.10k). http:/www. cs. princeton.edu/appel/modern/java/CUP/, 1999-09 3 Aho A V, Sethi R, Ullman J D. Compilers: Principles, Techniques, and Tools. Addison Wesley, 1986