Docsity

Grammar Syntax Analysis - Programming Language Compilers | CS 5363, Assignments of Computer Science

Partial preview of the text

Download Grammar Syntax Analysis - Programming Language Compilers | CS 5363 and more Assignments Computer Science in PDF only on Docsity!

Syntax Analysis

Context-free grammar

Top-down and bottom-up

parsing

Grammar Syntax Analysis

 Recognize the structure of programs

 Description of the language

 Context-free grammars

 Parsing: given an input string, discover the structure of

the string by constructing a parse tree

 Reject the input if it cannot be derived from the grammar

Parse tree for 5+1520:*

E ::= E + T | E – T | T
T ::= T * F | T / F | F

F ::= n

Grammar: (^) e

e

T
T
T
F
* F
F

Context-free Grammar

 A context-free grammar includes (T,NT,S,P)

 A set of tokens or terminals --- T

 Atomic symbols in the language

 A set of non-terminals --- NT

 Variables representing constructs in the language

 A set of productions --- P

 Rules identifying components of a construct

 BNF: each production has format A ::= B (or AB) where

 A is a single non-terminal

 B is a sequence of terminals and non-terminals

 A start non-terminal --- S

 The main construct of the language

Example: simple expressions

 BNF: a collection of production rules

e ::= n | e+e | e−e | e * e | e / e

 Non-terminals: e

 Terminal (token): n, +, -, *, /

 Start symbol: e

 Using CFG to describe regular expressions

 n ::= d n | d

 d ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

 Derivation: top-down replacement of non-terminals

 Each replacement follows a production rule

 One or more derivations for each program

 Example: derivations for 5 + 15 * 20

e=> ee* => e+ee* => 5+ee* => 5+15e=>5+15**

e=> e+e => 5+e => 5+ee* =>5 +15e* => 5+15*

Abstract Syntax vs. Concrete

Syntax

 Concrete syntax: the syntax programmers write

 Example: different notations of expressions

 Prefix + 5 * 15 20

 Infix 5 + 15 * 20

 Postfix 5 15 20 * +

 Abstract syntax: the syntax recognized by compilers

 Identifies only the meaningful components

 The operation

 The components of the operation

e

e

e

e

e

Parse Tree for

Abstract Syntax Tree for 5 + 15 * 20

Abstract syntax trees

 Condensed form of parse tree for representing language

constructs

 Operators and keywords do not appear as leaves

 They define the meaning of the interior (parent) node

 Chains of single productions may be collapsed

If-then-else

B
S1 S
S
IF B
THEN S1 ELSE S
E
E
+ T
T

cs5363 10

Ambiguous Grammars

 A grammar is syntactically ambiguous if

 some program has multiple parse trees

 Multiple choices of production rules during derivation

 Consequence of multiple parse trees

 Parse trees are used to interpret programs

 Multiple ways to interpret a program

e

e

e

e e

e

e

e

e 5

  • e

Parse trees:

Grammar: e ::= n | e+e | e−e | e * e | e / e

Sentence: 5 + 15 * 20

Which of the following correctly defines non-ambiguous CFG?

(1) A grammar where each sentence has a unique syntax tree by leftmost

derivation

(2) A grammar where each sentence has a unique syntax tree by any

derivation

Rewrite ambiguous Expressions

 Solution1: introduce precedence and associativity rules to dictate

the choices of applying production rules

 Original grammar

e ::= n | e+e | e−e | e * e | e / e

 Precedence and associativity

 * / >> + -

 All operators are left associative

 Derivation for n+n*n

 e=>e+e=>n+e=>n+ee=>n+ne=>n+n*n

 Solution2: rewrite production rules by introducing additional non-

terminals

 Alternative grammar

E ::= E + T | E – T | T

T ::= T * F | T / F | F

F ::= n

 Derivation for n + n * n

 E=>E+T=>T+T=>F+T=>n+T=>n+TF=>n+FF=>n+nF=>n+nn

 How to modify the grammar if

 + and - has high precedence than * and /

 All operators are right associative

Ambiguous grammar with

disambiguating rules

E ::= E + E | E * E | (E) | id

  • **Operator + has lower precedence than ***
  • Both + and * are left-associative
E ::= E + T | T
T ::= T * F | F

F ::= (E) | id

vs.

 More flexible specification

 Can change disambiguating rules without changing

productions

 More efficient implementation

 No need to reduce FTE, just to enforce disambiguating

rules

What languages can be defined by

CFG?

e ::= num | string | id | e+e

 Support both alternative (|) and recursion

 Cannot incorporate context information

 Cannot determine the type of variable names

 Declaration of variables is in the context (symbol table)

 Cannot ensure variables are always defined before used

int w;

0 = w;

for (w = 1; w < 100; w = 2w)

a = “c” + 3;

a = “c” + w

Top-down Parsing

 Start from the starting non-terminal, try to find a

left-most derivation

E ::= E + T | E – T | T
T ::= T * F | T / F | F

F ::= n

Grammar:

Left-most derivation for 5+1520-7:*

E  E-T  E+T-T  T+T-T
 F+T-T  5+T-T  5+TF-T  5+FF-T  5+15*F-T
 5+1520-T  5+1520-F  5+15*20-

Which production rule to use for each left-most non-terminal?

e

e

T
T
T
F
* F
F

e

- T
F

Recursive Descent Parsers

 Create a procedure (function) for each non-terminal S

 recognize the language described by S

 Use a suite of recursive functions to recognize the entire

language

E ::= E + T | E – T | T
T ::= T * F | T / F | F

F ::= n

Grammar:

void ParseE() {

if (use the first rule) {

ParseE();

if (getNextToken() != PLUS)

ErrorRecovery()

ParseT();

}

else if (use the second rule) {

…

}

else …

}

void ParseT() { …… }

void ParseF() { …… }

How to decide which

production rule to use?

  • Try every one and back track

•Which to try first?

•Select a single one based on the

next input token

Eliminating left recursion

 A grammar is left-recursive if it has a derivation AAα for

some string α

ν Infinite recursion without consuming any token  cannot be

parsed by recursive descent parsers even with backtracking

A::=Aα | β

A::= β A’

A’::= αA’ | ε

E ::= E + T | E – T | T
T ::= T * F | T / F | F

F ::= n

Grammar:

Grammar:

E ::= TE’

E’ ::= + TE’ | - TE’ | ε

T ::= FT’

T’::= *FT’ | / FT’ | ε

F ::= n

Problem: Left-recursion could involve multiple derivations

Algorithm: Eliminating left-recursion

Input: grammar G with no cyclic

derivations (e.g. A⇒… ⇒ A) or ε-

production (e.g., A::= ε)

Output: equivalent grammar with no

left recursion

Algorithm:

  1. Arrange the non-terminals in

some order A 1 ,A 2 ,…,An

  1. for i = 1 to n do

for j = 1 to i-1 to

replace each production

Ai::=Ajα where

Aj ::= β1 | β 2 | … | βk

with Ai ::= β 1 α| β 2 α|… |βkα

end

eliminate immediate left-

recursion for all Ai productions

end

Example: S ::= Aa | b

A ::= Ac | Sd

Example: S ::= Aa | b

A ::= Ac | Aad | bd

Example: S ::= Aa | b

A ::= bdA’ | A’

A’::= cA’ | adA’ | ε