Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Compiler ch2 Part1 Edit

The document discusses context-free grammars which are used to define the syntax of programming languages. It defines context-free grammars as having tokens, nonterminals, productions, and a start symbol. Grammars can be ambiguous or unambiguous. Parse trees are used to represent the structure of a language defined by a grammar. The precedence and associativity of operators must be defined to resolve ambiguities that can arise.

Uploaded by

gehan89638
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Compiler ch2 Part1 Edit

The document discusses context-free grammars which are used to define the syntax of programming languages. It defines context-free grammars as having tokens, nonterminals, productions, and a start symbol. Grammars can be ambiguous or unambiguous. Parse trees are used to represent the structure of a language defined by a grammar. The precedence and associativity of operators must be defined to resolve ambiguities that can arise.

Uploaded by

gehan89638
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 2

A Simple One-Pass Compiler

1
2.1 Overview
• Programming language:
– What its program look like (Syntax : context-free
grammars)
– What its program mean (Semantics : more difficult)

2
2.2 Syntax Definition
• Context-free grammar
• Grammar : hierarchical structure
– stmt if (expr) stmt else stmt
– production
– token: if, (, ), else
– nonterminal: expr, stmt

3
Components of Context-free
Grammar
1. A set of tokens (terminals)
• Digits
• Sign (+, -, <, =)
• if, while
2. A set of nonterminals
3. A set of productions
• nonterminal  ternimal/nonterminal
• left side  right side
4. First nonterminal symbol: start symbol

4
Example 2.1:
Grammars of expression ‘9-5+2’
• Example 2.1: grammars of expression ‘9-5+2’
list  list + digit
list  list – digit
list  digit
digit  0| 1| 2| 3| 4| 5| 6| 7| 8| 9
 list  list+digit | list-digit | digit
• nonterminal: list (start symbol), digit
• terminal (token): 0| 1| 2| 3| 4| 5| 6| 7| 8| 9

5
Example 2.1:
Grammars of expression ‘9-5+2’
• Token strings are derived from the start
symbol and repeatedly replacing a
nonterminal by the right side of a
production
• Empty string: 
• All possible token strings form the
language defined by the grammar

6
Example 2.2: Parse Tree
• Show how the start symbol derives a string
list  list + digit
list  list – digit
list  digit
digit  0| 1| 2| 3| 4| 5| 6| 7| 8| 9

7
Parse Trees
A

X Y Z
1. Root is labeled by start symbol
2. Each leaf is labeled by a token or 
3. Each interior is labeled by a nonterminal
4. If A is the nonterminal node and X1, X2,..Xn are the
labels of children of that node from left to right, then
A  X1, X2,..Xn , is a production

8
Yield of tree
• The leaves of a parse tree read from left to
right form the yield of the tree.

9
Ambiguity of A Grammar
• A grammar is said to be ambiguous if it can
have more than one parser tree generating a
given string.

10
Ambiguity of A Grammar:
Example
string  string+string | string-string
string  0|1|2|3|4|5|6|7|8|9

• Two expressions (9-5)+2 and 9-(5+2)

11
• Note: For compliling applications we need
to design unambigouous grammer or or use
ambiguous grammer with additional rules to
resolve ambiguity

12
Associativity of Operators
• Left Associative: 9+5-2  (9+5)-2
– +, -, *, /
– Parse tree grows down towards the left
• Right Associative: a=b=c  a=(b=c)
– Parse tree grows down towards the right

13
Associativity of Operators
right  letter = right | letter
letter  a|b|c|…|z

14
Precedence of Operators
• 9+5*2  9+(5*2)
• * , / has higher precedence than +, -
• *, /, +, - are all left associative
• term for *, /
– term  term * factor | term / factor | factor
• expr for +,-
– expr  expr + factor | expr – factor | factor
• factor  digit |(expr)

15
Precedence of Operators
• Syntax of expression
expr  expr + term | expr – term | term
term  term * factor | term / factor | factor
factor  digit |(expr)
• Syntax of statement for Pascal (ambiguous?)
stmt  id := expr | if expr then stmt
| if expr then stmt else stmt
| while expr do stmt
| begin opt_stmts end
16

You might also like