Syntax Trees | Compiler Design - Computer Science Engineering (CSE) PDF Download

2.2 SYNTAX TREES: 

Parse tree can be presented in a simplified form with only the relevant structure information by:

  •  Leaving out chains of derivations (whose sole purpose is to give operators difference precedence).
  • Labeling the nodes with the operators in question rather than a non-terminal. The simplified Parse tree is sometimes called as structural tree or syntax tree.
     

                                                    Syntax Trees | Compiler Design - Computer Science Engineering (CSE)  
 

Syntax Error Handling: If a compiler had to process only correct programs, its design & implementation would be greatly simplified. But programmers frequently write incorrect programs, and a good compiler should assist the programmer in identifying and locating errors.The programs contain errors at many different levels. For example, errors can be:
1) Lexical – such as misspelling an identifier, keyword or operator
2) Syntactic – such as an arithmetic expression with un-balanced parentheses.
3) Semantic – such as an operator applied to an incompatible operand.
4) Logical – such as an infinitely recursive call.

Much of error detection and recovery in a compiler is centered around the syntax analysis phase. The goals of error handler in a parser are:

  • It should report the presence of errors clearly and accurately.
  •  It should recover from each error quickly enough to be able to detect subsequent errors.
  •  It should not significantly slow down the processing of correct programs.

    2.3 Ambiguity:
    Several derivations will generate the same sentence, perhaps by applying the same productions in a different order. This alone is fine, but a problem arises if the same sentence has two distinct parse trees. A grammar is ambiguous if there is any sentence with more than one parse tree.

    Any parses for an ambiguous grammar has to choose somehow which tree to return. There are a number of solutions to this; the parser could pick one arbitrarily, or we can provide some hints about which to choose. Best of all is to rewrite the grammar so that it is not ambiguous. There is no general method for removing ambiguity. Ambiguity is acceptable in spoken languages. Ambiguous programming languages are useless unless the ambiguity can be resolved.

    Fixing some simple ambiguities in a grammar:
    Ambiguous language unambiguous (i) A → B | AA Lists of one or more B’s A → BC C → A | E (ii) A → B | A;A Lists of one or more B’s with punctuation A → BC C → ;A | E (iii) A → B | AA | E lists of zero or more B’s A → BA | E Any sentence with more than two variables, such as (arg, arg, arg) will have multiple parse trees.

 

Ambiguous language  unambiguous
(i) A → B | AA  Lists of one or more B’s
 C → A | E
(A → BC
(ii) A → B | A;ALists of one or more B’s with punctuation
C → ;A | E
A → BC
(iii) A → B | AA | Elists of zero or more B’sA → BA | E

Any sentence with more than two variables, such as (arg, arg, arg) will have multiple parse trees
 

2.4 Left Recursion: 
 

If there is any non terminal A, such that there is a derivation A The A ⇒α for some string⇒, then the grammar is left recursive.
 

Algorithm for eliminating left Recursion:

1. Group all the A productions together like this: A ⇒ A α1 | A α2 | - - - | A αm | β1 | β2 | - - - | βn
Where,
A is the left recursive non-terminal,
αis any string of terminals and
β is any string of terminals and non terminals that does not begin with A.

2. Replace the above A productions by the following: A ⇒ β1 A I |β2 A I | - - - |βn A I A I ⇒ ⇒1 A I | ⇒2 A I | - - - |⇒m A I | ∈ Where, AI is a new non terminal. Top down parsers cannot handle left recursive grammars.
 

If our expression grammar is left recursive:

  •  This can lead to non termination in a top-down parser.
  •  for a top-down parser, any recursion must be right recursion.
  •  we would like to convert the left recursion to right recursion.
     
The document Syntax Trees | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Syntax Trees - Compiler Design - Computer Science Engineering (CSE)

1. What is a syntax tree in computer science engineering?
Ans. A syntax tree, also known as a parse tree, is a visual representation of the syntactic structure of a sentence or a program in computer science engineering. It shows how the different elements of the sentence or program relate to each other based on the rules of a formal grammar.
2. How are syntax trees useful in computer science engineering?
Ans. Syntax trees are useful in computer science engineering as they provide a structured and hierarchical representation of the syntax of a sentence or a program. They help in understanding the relationship between different elements, identifying syntactic errors, and facilitating the process of parsing and analyzing code.
3. What are the components of a syntax tree in computer science engineering?
Ans. The components of a syntax tree in computer science engineering include nodes, edges, and labels. Nodes represent the different elements of the sentence or program, edges represent the syntactic relationships between these elements, and labels provide additional information about the elements or relationships.
4. How can syntax trees be constructed in computer science engineering?
Ans. Syntax trees can be constructed in computer science engineering using parsing algorithms such as top-down parsing or bottom-up parsing. These algorithms follow the rules of a formal grammar to analyze the sentence or program and build the tree structure based on the syntactic relationships between the elements.
5. What is the role of syntax trees in programming language compilers?
Ans. Syntax trees play a crucial role in programming language compilers. They are used to validate the syntax of the code, identify and report syntax errors, and generate intermediate representations of the code for further compilation or interpretation. Syntax trees also facilitate the optimization and analysis of the code during the compilation process.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Extra Questions

,

ppt

,

Free

,

Exam

,

Previous Year Questions with Solutions

,

Syntax Trees | Compiler Design - Computer Science Engineering (CSE)

,

video lectures

,

Viva Questions

,

Summary

,

mock tests for examination

,

shortcuts and tricks

,

Syntax Trees | Compiler Design - Computer Science Engineering (CSE)

,

Important questions

,

practice quizzes

,

study material

,

past year papers

,

Sample Paper

,

Semester Notes

,

pdf

,

Syntax Trees | Compiler Design - Computer Science Engineering (CSE)

,

Objective type Questions

,

MCQs

;