Docsity

Syntax Directed translation-Compiler Construction, Exams of Compiler Construction

Partial preview of the text

Download Syntax Directed translation-Compiler Construction and more Exams Compiler Construction in PDF only on Docsity!

Syntax Analysis

Part I

Chapter 4

The role of parser

Lexical Analyzer Parser Source program Symbol table Rest of Front End token getNext Token Parse tree (^) Intermediate representation

SyntaxDirected Translation

  • (^) One of the major roles of the parser is to produce

an intermediate representation (IR) of the source

program using syntaxdirected translation

methods

  • (^) Possible IR output:
    • (^) Abstract syntax trees (ASTs)
    • (^) Controlflow graphs (CFGs) with triples, threeaddress code, or register transfer list notation
    • (^) WHIRL (SGI Pro64 compiler) has 5 IR levels!

Error Handling

  • (^) A good compiler should assist in identifying and

locating errors

  • (^) Lexical errors: important, compiler can easily recover and continue, misspellings of tokens
  • (^) Syntax errors: most important for compiler, can almost always recover, misplaced semicolons, or extra or missing braces.
  • (^) Static semantic errors: important, can sometimes recover, type mismatched between operators and operands.
  • (^) Dynamic semantic errors: hard or impossible to detect at compile time, runtime checks are required
  • (^) Logical errors: hard or impossible to detect

Error Recovery Strategies

  • (^) Global correction
    • (^) Choose a minimal sequence of changes to obtain a global leastcost correction
    • (^) Unfortunately, these methods are in general too costly to implement in terms of time and space, so these techniques are currently only of theoretical interest.

Grammars (Recap)

• Contextfree grammar is a 4tuple

G = (N, T, P, S) where

  • (^) T is a finite set of tokens (terminal symbols)
  • (^) N is a finite set of nonterminals
  • (^) P is a finite set of productions of the form

where   (NT)* N (NT)* and   (NT)*

  • (^) S  N is a designated start symbol

Chomsky Hierarchy: Language

Classification

  • (^) A grammar G is said to be
    • (^) Regular if it is right linear where each production is of the form A  w B or A  w or left linear where each production is of the form A  B w or A  w
    • (^) Context free if each production is of the form A   where A  N and   (NT)*
    • (^) Context sensitive if each production is of the form  A      where A  N, ,,  (NT)*, || > 0
    • (^) Unrestricted

Chomsky Hierarchy

L(regular)  L(context free)  L(context sensitive)  L(unrestricted) Where L(T) = { L(G) | G is of type T } That is: the set of all languages generated by grammars G of type T L 1 = { a n b n | n  1 } is context free L 2 = { a n b n c n | n  1 } is context sensitive Every finite language is regular! (construct a FSA for strings in L(G)) Examples:

Derivations (Recap)

  • (^) The onestep derivation is defined by

 A     

where A   is a production in the grammar

  • (^) In addition, we define  (^)  is leftmost lm if  does not contain a nonterminal  (^)  is rightmost rm if  does not contain a nonterminal
    • (^) Transitive closure *^ (zero or more steps)
    • (^) Positive closure +^ (one or more steps)
  • (^) The language generated by G is defined by

L(G) = {w  T* | S +^ w}

Derivation (Example)

Grammar G = ({E}, {+,*,(,), ,id}, P, E) with productions P = E  E + E E  E * E E  ( E ) E  E E  id E  E  id E 

E E 

id * id + id

E 

rm

E + E 

rm E + id  rm id + id Example derivations: E 

id + id

TopDown Parsing

• LL methods (Lefttoright, Leftmost

derivation) and recursivedescent parsing

Grammar: E  T + T T  ( E ) T  E T  id Leftmost derivation: E  lm

T + T

lm id + T  lm id + id E E T

T

id id

E

T T

E

T

T

id

Problems for topdown parsing with backtracking : (1) leftrecursion (can cause a topdown parser to go into an infinite loop) Def. A grammar is said to be leftrecursive if it has a nonterminal A s.t. there is a derivation A ⇒ A  for some . (2) backtracking undo not only the movement but also the semantics entering in symbol table. (3) the order the alternatives are tried (For the grammar shown above, try w = cabd where A  a is applied first)

Ambiguity