Вы находитесь на странице: 1из 3

TITLE: Usage of YACC tool/ Parser generator

PROBLEM STATEMENT: Write a program to create syntax tree for given input arithmetic expression

THEORY: The syntax tree is an abstract representation of the language constructs. The syntax trees are used to write the translation routines using syntax-directed definitions. Syntax tree is a tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code. The syntax is 'abstract' in not representing every detail appearing in the real syntax. Syntax trees are a data structure widely used in compilers, due to their property of representing the structure of program code.It often serves as an intermediate representation of the program through several stages that the compiler requires, and has a strong impact on the final output of the compiler. The syntax tree is used intensively during semantic analysis, where the compiler checks for correct usage of the elements of the program and the language. Also, during semantic analysis the compiler generates the symbol tables based on the syntax tree. A complete traversal of the tree allows to verify the correctness of the program. After verifying the correctness, the syntax serves as the base for the code generation step. It is often the case that the syntax is used to generate the 'intermediate representation' '(IR)' for the code generation.

Constructing syntax tree for expressions means translation of expression into postfix form.The nodes for each operator and operand is created.Each node can be implemented as a record with multiple fields.

Following are the functions used in syntax tree for expression. 1.mknode(op,left,right):- This function creates a node with the field operator having operator having operator as label,and the two pointers to left and right.
+

2.mkleaf(id,entry):- This function creates an identifier node with label id and a pointer to symbol table is given by 'entry'.
Id

3.mkleaf(num,val):- This function creates node for number with label num and val is for value of that number.
num

Example: Construct the syntax tree for the expression x*y-5+z Step1: Convert expression from infix to postfix xy*5-z+. Step2: Make use of the functions mknode(),mkleaf(id,ptr),mkleaf(num,val). Step3: The sequence of function calls is given. Postfix Expression xy*5-z+ Symbol X Y * 5 Z + Operations P1= mkleaf(id,ptr to entry x) P2= mkleaf(id,ptr to entry y) P3=mknode(*,p1,p2) P4= mkleaf(num,5) P5=mknode(-,p3,p4) P6= mkleaf(id,ptr to entry z) P7=mknode(+,p5,p6)

Consider this string and draw the syntax tree: +

CONCLUSION: Thus, We successfully studied and implemented syntax tree for arithmetic expression.

Вам также может понравиться