L8-Yacc - Introduction to Compiler Design Yacc: The Parser...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Compiler Design Yacc: The Parser Generator Professor Yi-Ping You Department of Computer Science http://www.cs.nctu.edu.tw/~ypyou/ Page 1 Introduction to Compiler Design, Spring 2010
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Goal of Term Project ompiler Compiler LEX Java YACC P program bytecode Binary .class file Assembler for to run on JVM Java bytecode Page 2 Introduction to Compiler Design, Spring 2010
Background image of page 2
Compilation Flow Project 1 (lex) Project 2 (yacc) Project 3 (yacc) (Semantic Analyzer) Project 4 (yacc) Page 3 Introduction to Compiler Design, Spring 2010
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What is YACC? What is YACC ? ool which will produce a parser for a given grammar Tool which will produce a parser for a given grammar YACC (Yet Another Compiler-Compiler) is a program designed to compile a LALR(1) grammar and to gp ()g produce the source code of the syntactic analyzer of the language produced by this grammar Original written by Stephen C. Johnson, 1975 ariants: Variants: yacc (AT&T) ison: a acc placement (GNU) bison: a yacc replacement (GNU) BSD yacc CYACC ( braxas ftware) Page 4 Introduction to Compiler Design, Spring 2010 PCYACC (Abraxas Software)
Background image of page 4
A YACC Example stmt id := expr ; pr pr um um utput: Input: a := 3 + 5; %token ID ASSIGN PLUS NUMBER SEMI expr expr + num | num Output: reducing to expression from NUMBER… reducing to expression… reducing to statement… %% statement: ID ASSIGN expression SEMI {printf(“reducing to statement…\n”);} ; expression: expression PLUS NUMBER { $$ = $1 + $3; printf(“reducing to expression…\n”); } | NUMBER $$ $1 { $$ = $1; printf(“reducing to expression from NUMBER…\n”); } Page 5 Introduction to Compiler Design, Spring 2010 ; %%
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
YACC Source Program Yacc program is separated into three sections y % elimiters by %% delimiters The general format of Yacc source is (optional) {declarations} %% (required) {grammar rules} % (optional) %% {user subroutines} The absolute minimum Yacc program is %% Page 6 Introduction to Compiler Design, Spring 2010 S: ;
Background image of page 6
General Format of YACC Program %{ C declarations and includes %} %token <name1> <name2> … Declarations %start <symbol> %% <grammar rule> <action> <grammar rule> <action> % Rules %% User subroutines (C code) Routines Page 7 Introduction to Compiler Design, Spring 2010
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Grammar Rule Section Each rule contains LHS and RHS , separated by a olon nd end by a micolon colon and end by a semicolon White spaces or tabs are allowed ctions may be associated with rules and are Actions may be associated with rules and are executed when the associated production is duced reduced E.g., stmt id := expr | expr stmt: ID ASSIGN expr {<C code>}; stmt: expr {<C code>}; stmt: ID ASSIGN expr {<C code>} xpr <C code>} Page 8 Introduction to Compiler Design, Spring 2010 | expr {C code } ;
Background image of page 8
YACC Actions Actions are C code ctions can include references to attributes Actions can include references to attributes associated with terminals and non-terminals in the productions Actions may be put inside a rule Action performed when symbol is pushed on stack E.g., A : B {<action1>} C {action2}; ACT: {<action1>}; :BACTC{action2}; Safest (i.e. most predictable) place to put action A : B ACT C {action2}; Page 9 Introduction to Compiler Design, Spring 2010 is at end of rule
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 37

L8-Yacc - Introduction to Compiler Design Yacc: The Parser...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online