0% found this document useful (0 votes)
2 views13 pages

Lect-03

The document discusses the role of parsers in modern compilers, focusing on their ability to recognize context-free syntax and guide semantic analysis. It explains the structure of Context-Free Grammars (CFG) and provides examples of grammar for expressions and parse trees. Additionally, it introduces the concept of Abstract Syntax Trees (ASTs) as a more concise representation of grammatical structure used in the compilation process.

Uploaded by

liw73157
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Lect-03

The document discusses the role of parsers in modern compilers, focusing on their ability to recognize context-free syntax and guide semantic analysis. It explains the structure of Context-Free Grammars (CFG) and provides examples of grammar for expressions and parse trees. Additionally, it introduces the concept of Abstract Syntax Trees (ASTs) as a more concise representation of grammatical structure used in the compilation process.

Uploaded by

liw73157
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Modern Compilers

Parser

 Recognizes context free syntax and reports errors.


 Guides context sensitive (semantic) analysis
 Builds IR for source code
Parser – Context Free Grammar
 The syntax of most programming languages is
specified using Context-Free Grammars (CFG)
 Context- free syntax is specified with a grammar
G=(S,N,T,P) where.
– S is the start symbol
– N is a set of non-terminal symbols
– T is set of terminal symbols or words
– P is a set of productions or rewrite rules
 CFG for palindromes?

A string with 0110


Context Free Grammar – Grammar of
Expressions

1. goal  expr For this CFG


2. expr  expr op term S = goal
3. | term

4. term  number T = { number, id, +, -}


5. | id
N = { goal, expr, term, op}
6. op  +

7. |–
P = { 1, 2, 3, 4, 5, 6, 7}
CFG - Grammar of Expressions: Example
 Given a CFG, we can derive sentences by repeated substitution.
Consider the sentence x + 2 – y

1. goal  expr

2. expr  expr op term

3. | term

4. term  number

5. | id

6. op  +

7. |–
CFG - Grammar of Expressions: Example

 To recognize a valid sentence in some CFG,


we reverse this process and build up a parse,
thus the name “parser”.
Front End – Parse Tree

A parse can be represented by a tree: parse


tree or syntax tree.
 For example, here is the parse tree for the
expression x+2-y
Abstract Syntax Tree
 The parse tree contains a lot of unneeded
information

 Compilers often use an


– Abstract Syntax Tree (AST)

Concrete Parse Tree


Abstract Syntax Tree

 This is much more concise


 AST summarizes grammatical structure without the details
of derivation
Abstract Syntax Tree

 ASTs are kind of Intermediate Representation (IR)


The Backend

You might also like