edited by
720 views
1 votes
1 votes

Consider the following grammar for variable declarations:

  • <vardecl> $\rightarrow$ <vardecl><idlist> : <type>;
  • <vardecl> $\rightarrow \in$
  • <idlist> $\rightarrow$ <idlist>, id
  • <idlist> $\rightarrow$ id
  • <type> $\rightarrow$ integer
  • <type> $\rightarrow$ real

Write a syntax - directed translation scheme to install the identifiers into the symbol table. You must also issue appropriate error messages wherever necessary. Make suitable assumptions regarding procedures operating on the symbol table; you need not elaborate upon these procedures.

edited by

1 Answer

0 votes
0 votes

First, we'll define some attributes to help us carry information during the parsing process:

  1. VarDecl.attribute: This attribute will represent the type of the variable declaration.
  2. 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!

Related questions

1 votes
1 votes
1 answer
1
makhdoom ghaya asked Nov 30, 2016
725 views
Consider the definition of macro $B,$ nested within the definition of a macro $A.$ Can a call to macro $B$ also appear within macro $A?$ If not, why not? If yes, explain ...
3 votes
3 votes
1 answer
4
makhdoom ghaya asked Nov 30, 2016
774 views
Express the following list in terms of a linked list structure suitable for internal representation.$(((ab)c)d((e)))$