Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building The Parse Tree © Allan C. Milne Abertay University v14.6.18.

Similar presentations


Presentation on theme: "Building The Parse Tree © Allan C. Milne Abertay University v14.6.18."— Presentation transcript:

1

2 Building The Parse Tree © Allan C. Milne Abertay University v14.6.18

3 Agenda. Yacc and the parse tree. Rules and semantic values. Builder functions. Worked-through example.

4 Yacc and the parse tree. Your Yacc program must build up the parse tree representing the structure of the input program. The rules of your Yacc program are now responsible for building up parse tree branches; –no artifact generation is performed within these rules. Artifact generation is performed once the entire input program has been parsed and its parse tree built; –perform this task by traversing the parse tree representation of the input program.

5 Rule responsibility. Each Yacc rule returns a semantic value that is the branch of the tree representing the parse of that non-terminal’s rule structure; –i.e. the elements of the particular production applied to parse the particular input program. A terminal is represented by a parse tree node with no structure branch; –i.e. a leaf node. –‘Noise’ terminals need not be included in the parse tree.

6 The parse tree representation. See last week’s lecture for more details.See last week’s lecture Parse tree branch is a pointer to a tree node. The struct representing the tree nodes is defined in ParseTree.h.

7 Yacc semantic values. Yacc rules must return a parse tree branch as their semantic value. This branch is denoted by a pointer to a parse tree node. The possible semantic values used within the compiler must be defined via the %union definition. %union { double number; /* numeric literals. */ char* string; /* id names and string literals. */ struct treeNode* parseTree; /* parse tree branches. */ }

8 … and tell the rules … The semantic value type to be returned by each rule must be defined via appropriate %type definitions. %type statementSequence %type statement......... %type expression

9 Building the parse tree. The parse tree is built by direction from the Yacc rules; –building new branches from the branches of the elements of the production being applied. Define functions that return the pointer to the branch created; –one function for each non-terminal; –wrap in ParseTreeBuilder component; ParseTreeBuilder.h ParseTreeBuilder.c

10 Example … Use pseudo-variables as before. generator: tGENERATE expression type tVALUES tFROM range { $$ = newGenerate ($2, $3, $6); } ;

11 Builder function. Creates branch representing ‘generate' statement. parseNode* newGenerate ( parseNode *specifier, parseNode *valueType, parseNode *range) { parseNode *node = newNode (NTGenerator); node->structure = specifier; specifier->next = valueType; valueType->next = range; return node; }

12 Creating a new node. This helper function allocates space for a new parse tree node; –Initializes to 0; and –sets its type. parseNode* newNode (int nodeType) { parseNode *node = (parseNode*)malloc (sizeof(parseNode)); memset ((void*)node, 0, sizeof (parseNode)); node->type = nodeType; return node; }

13 Following it through … expression: tNUMBER { $$ = newNumExpr ($1); } |... parseNode* newNumExpr (double val) { parseNode *node = newNode (NTExpression); node->structure = newNumber (val); return node; } parseNode* newNumber(double val) { parseNode *node = newNode (NTNumber); node->value.dblValue = val; return node; }

14 … and so we have … Continue working through from GenVal.y through GenVal.l to ParseTreeBuilder.c for the and elements. tNUMBER (2) ……


Download ppt "Building The Parse Tree © Allan C. Milne Abertay University v14.6.18."

Similar presentations


Ads by Google