cpsc 325 - compiler

16
CPSC 325 - Compiler Tutorial 3 Parser

Upload: awentia-jackson

Post on 01-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

CPSC 325 - Compiler. Tutorial 3 Parser. Parsing. Input. The syntax of most programming languages can be specified by a Context-free Grammar (CGF) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CPSC 325 - Compiler

CPSC 325 - Compiler

Tutorial 3

Parser

Page 2: CPSC 325 - Compiler

Parsing

The syntax of most programming languages can be specified by a Context-free Grammar (CGF)

Parsing: Given a grammar and a sentence, traverse the derivation (parse tree) for the sentence in some standard order and do something useful at each node.

Input

Page 3: CPSC 325 - Compiler

Example

program

program statement

statement

assignStmt

id

int

expr

ifStmt

statementexpr

expr expr

id int

assignStmt

idexpr

int

program ::= statement | program statementstatement ::= assignStmt | ifStmtassignStmt ::= id = expr;ifStmt ::= if ( expr ) statementexpr ::= id | int | expr + exprid ::= a | b | c | i | … | zint ::= 0 | 1 | … | 9

a = 1 ; if ( a + 1 )b = 2 ;

Page 4: CPSC 325 - Compiler

Standard Order

When we write a parser, We want the it to be deterministic (no backtracking), and examine the source program from left to right.

– (Parse the program in linear time in the order it appears in the source file)

Page 5: CPSC 325 - Compiler

Parsing

Top-down– Start with the root– Traverse the parse tree depth-first, left-to-right– Left recursive is evil. (example of if-else)

Bottom-up– Start at leaves and build up to the root

Page 6: CPSC 325 - Compiler

Something Useful

At each point (node) in the traversal, perform some semantic action:– Construct nodes of full parse tree (rare)– Construct abstract syntax tree (common)– Construct linear, lower-level representation (more

common)– Generate target code on the fly (1-pass compiler;

not common in production compilers – can’t generate very good code in one pass)

Page 7: CPSC 325 - Compiler

Context-Free Grammars

Formally, a grammar G is a 4-tuples <N,T,P,S> where– N: a finite set of non-terminal symbols– T: a finite set of terminal symbols– P: A finite set of productions– S: the start symbol, a distinguished element of N

α A γ => α β γ iff A ::= β in P

Page 8: CPSC 325 - Compiler

Reduced Grammars

• Grammar G is reduced iff there is no useless production in G.

Page 9: CPSC 325 - Compiler

Ambiguity

Grammar G is unambiguous iff every sentence in L(G) has a unique leftmost (or rightmost) derivation

– Fact: unique leftmost or unique rightmost implies the other

A grammar without this property is ambiguous– Note that other grammars that generate the same language

may be unambigious

We need unambiguous grammars for parsing

Page 10: CPSC 325 - Compiler

Example

expr ::= expr + expr | expr – expr | expr * expr | expr / expr | int

int ::= 0 | 1 | 2 | … | 9

Exercise: Show that this is ambiguousHow? Show two different leftmost or right most derivations for the same stringEquivalently: show two different parse trees for the same string

Page 11: CPSC 325 - Compiler

Example (cont)

Give a leftmost derivation of 2+3*4 and show the parse tree.

Give two different derivations of 7+3+1

Page 12: CPSC 325 - Compiler

Problem?

The grammar has no notion of precedence or associatively

Solution:– Create a non-terminal for each level of

precedence– Isolate the corresponding part of the grammar– Force the parser to recognize higher precedence

sub expressions first

Page 13: CPSC 325 - Compiler

Classic Expression Grammar

expr ::= expr + term | expr – term | term term ::= term * factor | term / factor | factor factor ::= int | ( expr ) int ::= 0 | 1 | 2 | … | 9

Check 7 + 3 + 2 Check (5 + 3) * 2

Page 14: CPSC 325 - Compiler

Another Classic example

Grammar for conditional statements......

stmt ::= ifStmt | whileStmt

ifStmt ::= if ( cond ) stmt

| if ( cond ) stmt lese stmt

……

Is this grammar ok?

Page 15: CPSC 325 - Compiler

Solving Ambiguity

Fix the grammar to separate if statements with else clause and if statement with no elxse

- add lots of non-terminals

Use some ad-hoc rule in parser

- “else matches closest unpaired if”

Page 16: CPSC 325 - Compiler

Parser tools

Most parser tools can cope with ambiguous grammars

Be sure that what the tool does is really what you want.

next week we will talk about Bison and more Parsers.