Abstract Syntax Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc04.html.

Slides:



Advertisements
Similar presentations
Chapter 3 Syntax Analysis
Advertisements

Abstract Syntax Mooly Sagiv html:// 1.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Mooly Sagiv and Roman Manevich School of Computer Science
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Abstract Syntax Trees Compiler Baojian Hua
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
ML-YACC David Walker COS 320. Outline Last Week –Introduction to Lexing, CFGs, and Parsing Today: –More parsing: automatic parser generation via ML-Yacc.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Context-Free Grammars Lecture 7
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Semantic Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
Chapter 2 A Simple Compiler
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Semantic Analysis Mooly Sagiv html://
Parser construction tools: YACC
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Syntax and Semantics Structure of programming languages.
LEX and YACC work as a team
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
1 Week 3 Questions / Concerns What’s due: Lab1b due Friday at midnight Lab1b check-off next week (schedule will be announced on Monday) Homework #2 due.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lab 3: Using ML-Yacc Zhong Zhuang
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial) 1.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Abstract Syntax Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Abstract Syntax Trees Compiler Baojian Hua
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Bernd Fischer COMP2010: Compiler Engineering Abstract Syntax Trees.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Programming Languages Translator
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Textbook:Modern Compiler Design
CS 614: Theory and Construction of Compilers
Bison: Parser Generator
Bison Marcin Zubrowski.
Presentation transcript:

Abstract Syntax Mooly Sagiv html://

Outline The general idea Bison Motivating example Interpreter for arithmetic expressions The need for abstract syntax Abstract syntax for arithmetic expressions

Semantic Analysis during Recursive Descent Parsing Scanner returns “semantic values” for some tokens The function of every non-terminal returns the “corresponding subtree value” When A  B C D is applied the function for A can use the values returned by B, C, and D –The function can also pass parameters, e.g., to D(), reflecting left contexts

E  num E’ E’   E’  + num E’ int E() { switch (tok) { case num : temp=tok.val; eat(num); return EP(temp); default: error(…); }} int EP(int left) { switch (tok) { case $: return left; case + : eat(+); temp=tok.val; eat(num); return EP(left + temp); default: error(…) ;}}

Semantic Analysis during Bottom-Up Parsing Scanner returns “semantic values” for some tokens Use parser stack to store the “corresponding subtree values” When A  B C D is reduced the function for A can use the values returned by B, C, and D No action in the middle of the rule

E  E + num E  num Example E 7 + num 5 E 12

Bison Specification Declarations % Productions % C -Routines

Interpreter (in Bison) %{ declarations of yylex() and yyeror() %} %union { int num; string id;} %token ID %token NUM %type e f t %start e % e : e ‘+’ t {$$ = $1 + $3 ; } | e ‘-’ t { $$ = $1 - $3 ; } | t { $$ = $1; } ; t : t ‘*’ f { $$ = $1 * $3; } | t ‘/’ f { $$= $1 / $3; } | f { $$ = $1; } ; f : NUM { $$ = $1; } | ID { $$ = lookup($1); } | ‘-’ e { $$ = - $2; } | ‘(‘ e ‘)’ { $$ = $2; } ;

Interpreter (compact spec.) %{ declarations of yylex() and yyeror() %} %union { int num; string id;} %token ID %token NUM %type e %start e %left PLUS MINUS %left MUL DIV %right UMINUS % e : e PLUS e {$$ = $1 + $3 ; } | e MINUS e { $$ = $1 - $3 ; } | e MUL e { $$ = $1 * $3; } | e DIV e { $$= $1 / $3; } | NUM | ID { $$ = lookup($1); } | MINUS e % prec UMINUS { $$ = - $2; } | ‘(‘ e ‘)’ { $$ = $2; } ;

stackinputaction $ $shift num 7 $ $ reduce e  num e 7 $ $shift + e 7 $ 11+17$shift num 11 + e 7 $ +17$ reduce e  num

stackinputaction e 11 + e 7 $ +17$ reduce e  e+e e 18 $ +17$shift + e 18 $ 17$shift num 17 + e 18 $ $ reduce e  num

stackinputaction e 17 + e 18 $ $ reduce e::= e+e e 35 $ $ accept

So why can’t we write all the compiler code in Bison/CUP?

Historical Perspective Originally parsers were written w/o tools yacc, bison,... make tools acceptable But it is still difficult to write compilers in parser actions (top-down and bottom-up) –Natural grammars are ambiguous –No modularity principle –Many useful programming language features prevent code generation while parsing Use before declaration gotos

Abstract Syntax Intermediate program representation Defines a tree - Preserves program hierarchy Generated by the parser Declared using an (ambiguous) context free grammar (relatively flat) –Not meant for parsing Keywords and punctuation symbols are not stored (Not relevant once the tree exists) Big programs can be also handled (possibly via virtual memory)

Issues Concrete vs. Abstract syntax tree Need to store concrete source position Abstract syntax can be defined by: –Ambiguous context free grammar –C recursive data type –“Constructor” functions Debugging routines linearize the tree

Abstract Syntax for Arithmetic Expressions Exp  id (IdExp) Exp  num (NumExp) Exp  Exp Binop Exp (BinOpExp) Binop  + (Plus) Binop  - (Minus) Binop  * Binop  / (Times) (Div) Exp  Unop Exp (UnOpExp) Unop  - (UnMin)

/* absyn.h */ typedef enum {A_Plus, A_Minus, A_Times, A_Div} Binop; typedef enum {A_UnMin} Unop; typedef enum {A_IdExp, A_NumExp, A_BinOpExp, A_UnopExp} ExpKind; typedef struct exp { ExpKind kind ; union { struct {string repr ;} IdExp; struct {int number ; } NumExp; struct {Binop op; struct exp *left; struct exp *right;} BinExp; struct {Unop op; struct exp *arg;} UnExp; } exp; } *Exp; extern Exp idExp(string repr); extern Exp numExp(int number); extern Exp binOp(Binop, struct exp *, struct exp*); extern Exp unOp(Unop, struct exp*);

%{ declarations of yylex() and yyeror() #include “absyn.h” %} %union { int num; string id; Exp exp;} %token ID %token NUM %type e f t %start e % e : e ‘+’ t {$$ = binop(A_Plus, $1, $3) ; } | e ‘-’ t { $$ = binop(A_Minus, $1, $3) ; } | t { $$ = $1; } ; t : t ‘*’ f { $$ = binop(A_Times,$1, $3); } | t ‘/’ f { $$= binop(A_Div, $1, $3); } | f { $$ = $1; } ; f : NUM { $$ = numExp($1); } | ID { $$ = idExp($1); } | ‘-’ e { $$ = unOp(A_UnMinus, $2); } | ‘(‘ e ‘)’ { $$ = $2; } ;

%{ declarations of yylex() and yyeror() #include “absyn.h” %} %union { int num; string id; Exp exp; } %token ID %token NUM %type e %start e %left PLUS MINUS %left MUL DIV %right UMINUS % e : e PLUS e {$$ = binop(A_Plus, $1, $3) ; } | e MINUS e { $$ = binop(A_Minus, $1, $3) ; } | e MUL e { $$ = binop(A_Times, $1, $3); } | e DIV e { $$= binop(A_Div, $1, $3); } | NUM {$$ = numExp($1); } | ID { $$ = idExp($1); } | MINUS e % prec UMINUS { $$ = unOp(A_UnMin,$2); } | ‘(‘ e ‘)’ { $$ = $2; } ;

Summary Flex and Bison simplify the task of writing compiler/interpreter front-ends Abstract syntax provides a clear interface with other compiler phases –Supports general programming languages But the design of an abstract syntax for a given PL may take some time