Introduction to Yacc YingHung Jiang compilercsie ntu edu

  • Slides: 13
Download presentation
Introduction to Yacc Ying-Hung Jiang compiler@csie. ntu. edu. tw

Introduction to Yacc Ying-Hung Jiang compiler@csie. ntu. edu. tw

Outlines l l l Review of parser Introduction to yacc Cooperate with lex Semantic

Outlines l l l Review of parser Introduction to yacc Cooperate with lex Semantic routines Resources

Review of Parser l Parser invokes scanner for tokens. Parser analyze the syntactic structure

Review of Parser l Parser invokes scanner for tokens. Parser analyze the syntactic structure according to grammars. Finally, parser executes the semantic routines. l See teacher’s slide l l

Introduction to yacc l l l Yacc – yet another compiler. An LALR(1) parser

Introduction to yacc l l l Yacc – yet another compiler. An LALR(1) parser generator. Yacc generates – – – Tables – according to the grammar rules. Driver routines – in C programming language. y. output – a report file.

Cooperate with lex l l Invokes yylex() automatically. Generate y. tab. h file through

Cooperate with lex l l Invokes yylex() automatically. Generate y. tab. h file through the -d option. The lex input file must contains y. tab. h For each token that lex recognized, a number is returned (from yylex() function. )

Writing Yacc Input File l A Yacc input file consists of three sections: –

Writing Yacc Input File l A Yacc input file consists of three sections: – – – Definition Rules User code l Separate by %% l Similar to lex (actually, lex imitates yacc. )

Definition Section l l l C source code, include files, etc. %token Yacc invokes

Definition Section l l l C source code, include files, etc. %token Yacc invokes yylex() when a token is required. All terminal symbols should be declared through %token. Yacc produces y. tab. h by %token definitions.

Definition Section l %union { } – l l Default: int Yacc produces C

Definition Section l %union { } – l l Default: int Yacc produces C source where YYSTYPE is the type declared by %union. All symbols, include terminal and nonterminal symbols are of type YYSTYPE. %start Default start rule is the first one in rules section.

Rules Section l l l Each rule contains LHS and RHS, separated by a

Rules Section l l l Each rule contains LHS and RHS, separated by a colon and end by a semicolon. White spaces or tabs are allowed. Ex: statement: name EUQALSIGN expression | expression ; expression: number PLUSSIGN number | number MINUSSIGN number ;

Writing Grammar Rules l l Lex & yacc, 2/e. O’REILLY, 1999 The C Programming

Writing Grammar Rules l l Lex & yacc, 2/e. O’REILLY, 1999 The C Programming Language, Prentice-Hall, 1988 The C++ Programming Language, Addison. Wesley, 1991 Some language specifications. Ex: ANSI C, C 99, C++98

Semantic Routines l The action in semantic routines are executed for the production rule.

Semantic Routines l The action in semantic routines are executed for the production rule. The action is actually C source code. LHS: $$ RHS: $1 $2 …… Default action: { $$ = $1; } l Action between a rule is allowed. For ex: l l l expression : simple_expression | simple_expression {somefunc($1); } relop simple_expression;

Example l See the calculator example.

Example l See the calculator example.

Resources l See course website.

Resources l See course website.