2. Consider the following grammar: "= (D) ID TYPE; | ε (S) ID (ID); | ε As usual exactly all the nonterminals are surrounded by angle brackets ((...)) and every- thing else is a terminal. The lexical analysis provides the attribute name for each ID and the attribute type for each TYPE. Both attributes are of type string. The possible values for TYPE are "var" and "fun". A previous parse process has produced an AST according to this grammar of type Node* where Node is defined as follows: enum symbol { NT_P, NT_D, NT_S, T_TYPE, T_ID }; class Node { }; public: symbol type; // the type of the node string name; // the name of the ID, LIT, or TYPE (if applicable) Node* operator () (int i); // returns the i-th child of the node A node of type NT_P has two children of types NT_D and NT_S. A note of type NT_D has three children of type NT_D, T_ID, and T_TYPE; the first child may be NULL (or 0). Finally a node of type NT_S has three children of type NT_S, T_ID, and T_ID; the first child may also be NULL. The symbol table is defined as follows: typedef string name; typedef string type; class SymbolTable { public: }; type lookup (name n); // finds the name n in the table and // returns its type or ""if not found void insert (name n, type t); // inserts a new pair (n, t) in the table SymbolTable sym; Implement a type checker for an ASTs as described above, as follows: (a) ensure that each ID is declared before being used, and (b) ensure that in each call ID (ID) the first ID is a function and the second ID is a variable. Do not implement the main function but assume that such a main function initializes sym to an empty table and then performs the call typeCheck (root) where root is the root of the AST to be checked.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter12: Adding Functionality To Your Classes
Section12.5: Virtual Functions
Problem 5E
icon
Related questions
Question
2.
Consider the following grammar:
"=
(D) ID
TYPE; | ε
(S) ID (ID); | ε
As usual exactly all the nonterminals are surrounded by angle brackets ((...)) and every-
thing else is a terminal. The lexical analysis provides the attribute name for each ID and the
attribute type for each TYPE. Both attributes are of type string. The possible values for
TYPE are "var" and "fun".
A previous parse process has produced an AST according to this grammar of type Node*
where Node is defined as follows:
enum symbol { NT_P, NT_D, NT_S, T_TYPE, T_ID };
class Node {
};
public:
symbol type;
// the type of the node
string name; // the name of the ID, LIT, or TYPE (if applicable)
Node* operator () (int i); // returns the i-th child of the node
A node of type NT_P has two children of types NT_D and NT_S. A note of type NT_D has three
children of type NT_D, T_ID, and T_TYPE; the first child may be NULL (or 0). Finally a node of
type NT_S has three children of type NT_S, T_ID, and T_ID; the first child may also be NULL.
The symbol table is defined as follows:
typedef string name;
typedef string type;
class SymbolTable {
public:
};
type lookup (name n); // finds the name n in the table and
// returns its type or ""if not found
void insert (name n, type t); // inserts a new pair (n, t) in the table
SymbolTable sym;
Implement a type checker for an ASTs as described above, as follows: (a) ensure that each ID
is declared before being used, and (b) ensure that in each call ID (ID) the first ID is a function
and the second ID is a variable.
Do not implement the main function but assume that such a main function initializes sym to
an empty table and then performs the call typeCheck (root) where root is the root of the
AST to be checked.
Transcribed Image Text:2. Consider the following grammar: "= (D) ID TYPE; | ε (S) ID (ID); | ε As usual exactly all the nonterminals are surrounded by angle brackets ((...)) and every- thing else is a terminal. The lexical analysis provides the attribute name for each ID and the attribute type for each TYPE. Both attributes are of type string. The possible values for TYPE are "var" and "fun". A previous parse process has produced an AST according to this grammar of type Node* where Node is defined as follows: enum symbol { NT_P, NT_D, NT_S, T_TYPE, T_ID }; class Node { }; public: symbol type; // the type of the node string name; // the name of the ID, LIT, or TYPE (if applicable) Node* operator () (int i); // returns the i-th child of the node A node of type NT_P has two children of types NT_D and NT_S. A note of type NT_D has three children of type NT_D, T_ID, and T_TYPE; the first child may be NULL (or 0). Finally a node of type NT_S has three children of type NT_S, T_ID, and T_ID; the first child may also be NULL. The symbol table is defined as follows: typedef string name; typedef string type; class SymbolTable { public: }; type lookup (name n); // finds the name n in the table and // returns its type or ""if not found void insert (name n, type t); // inserts a new pair (n, t) in the table SymbolTable sym; Implement a type checker for an ASTs as described above, as follows: (a) ensure that each ID is declared before being used, and (b) ensure that in each call ID (ID) the first ID is a function and the second ID is a variable. Do not implement the main function but assume that such a main function initializes sym to an empty table and then performs the call typeCheck (root) where root is the root of the AST to be checked.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
CMPTR
CMPTR
Computer Science
ISBN:
9781337681872
Author:
PINARD
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:
9781337569798
Author:
ECKERT
Publisher:
CENGAGE L
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning