Get input character stream from tokenizer, then build the parse tree. (PT)
Parent node to create an “empty” child node
Pass the row number corresponding to the newly created node as argument
- Example: ParseIf() --
- Call getToken(), check it is an 'if'
- If not, print error and stop
- Call getToken(), check it is an 'if'
- Example: ParseStmt() --
- Call getToken, return a number, and apply one-token look ahead
- Call corresponding alternative, e.g. ParseIf()
- If the getToken returns an identifier, call parseAssign();
- Example: ParseExp() -- "9-5+4"
- Check if current node is a <fac>
- PT[n,0] = type_fac
- PT[n,2] = nextRow++; // next free row in PT[]
- Call ParseFac(PT[n,2]), create 1st child node
- Call getToken() and check its type
- if it is a '+' -- PT[n,1] = 2
- if it is a '-' -- PT[n,1] = 3
- Otherwise -- PT[n,1] = 1, return
- if PT[n,1] is 2/3, skipToken()
- PT[n,3] = nextRow++; // next free row in PT[]
- Call ParseExp(PT[n,3])
- return;
- Example: ParseFac()
- Check if current node is an <op>
- PT[n,0] = type_op
- Call ParseOp()
- Call getToken()
- if the returned value is a '*', -- PT[n,1] = 2, skipToken()
- if the returned value is not a '*', -- PT[n,1] = 1, return
- Call parseFac() if it is the 2nd alternative, then return
- ParseAssign() --
-
Check if the node is an identifier PT[n,0] = 7; // this is an <assign> node, construct the row and skip that token (for now)
-
PT[n,2] = nextRow++; // next free row in PT[]
- Call ParseID(PT[n,2]) -- the identifier to the left of the '='
- Now curser at '=', and check if it is actually a '=', then skipToken()
-
PT[n,3] = nextRow++; // next free row in PT[]
- Now parse the expression on the right of the '=' -- parseExp()
-
if (s != ‘‘;’’} {**Quit!!**} else {T.skipToken();}; return; }
-
ParseID()
- Call getToken() and check if it is an ID
- Call IdName of the identifier
- Check the global variable to know if we are parsing <decl seq> or <stmt seq>
- Get the string, and look up that string in the IdName table to see if it is there, and make decision based on that
- ParseID() construct the ID node and the put the id in the IdName table
- Skip pass the identifier -- skipToken()