First, we'll define some attributes to help us carry information during the parsing process:
- VarDecl.attribute: This attribute will represent the type of the variable declaration.
- IdList.attribute: This attribute will represent the identifier being processed.
Now, Syntax Directed Translation Rule as follows:
1. <vardecl> → <vardecl> <idlist> : <type>; { addTypeToSymbolTable(<idlist.attribute>, <type.attribute>); }
2. <vardecl> → <idlist> : <type>; { addTypeToSymbolTable(<idlist.attribute>, <type.attribute>); }
3. <idlist> → <idlist>, id { <idlist.attribute> = mergeIdLists(<idlist.attribute>, id.attribute); }
4. <idlist> → id { <idlist.attribute> = createIdList(id.attribute); }
5. <type> → integer { <type.attribute> = "integer"; }
6. <type> → real { <type.attribute> = "real"; }
- Rule 1 and Rule 2 handle variable declarations and call a function
addTypeToSymbolTable
to add the type information to the symbol table for each identifier in the list.
- Rule 3 and Rule 4 deal with the identifiers in the list. They call a function
mergeIdLists
to combine the attributes of identifiers in a comma-separated list.
- Rule 5 and Rule 6 set the attribute for the type.
Hope this helps!