Class Parser
java.lang.Object
Parser
Recursive descent parser for the YaLCC language.
Constructs a parse tree based on the provided lexical analyzer.
Each parsing method corresponds to a grammar production.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate SymbolThe current token being analyzedprivate final LexicalAnalyzerThe lexical analyzer providing tokens for parsing. -
Constructor Summary
ConstructorsConstructorDescriptionParser(LexicalAnalyzer lexer) Creates a parser with the given lexical analyzer. -
Method Summary
Modifier and TypeMethodDescriptionprivate Symboldummy(NonTerminal nt) Creates a dummy symbol for non-terminal nodes in the parse tree.private voidmatch(LexicalUnit expected) Matches the current token against the expected token type.private ParseTreeParse assignment statement.private ParseTreeParse code block.private ParseTreeParse condition.private ParseTreeParse condition atom.private ParseTreeParse condition comparison.private ParseTreeParse condition with implication.private ParseTreeParse addition and subtraction.private ParseTreeParse arithmetic expression.private ParseTreeParse multiplication and division.private ParseTreeParse primary expression.private ParseTreeParse unary expression.private ParseTreeparseIf()Parse If statement.private ParseTreeParse Input statement.private ParseTreeParse instruction statement.private voidparseOptionalElse(ParseTree node) Handles the optional Else branch in an If statement.private ParseTreeParse Output statement.Parse the entire program.private ParseTreeParse While statement.
-
Field Details
-
lexer
The lexical analyzer providing tokens for parsing. -
currentToken
The current token being analyzed
-
-
Constructor Details
-
Parser
Creates a parser with the given lexical analyzer.- Parameters:
lexer- the lexical analyzer- Throws:
Exception- if an error occurs during token retrievalIOException
-
-
Method Details
-
match
Matches the current token against the expected token type. Advances to the next token if matched; throws an error otherwise.- Parameters:
expected- the expected token type- Throws:
ParseException- if the current token does not match the expected typeIOException- if an I/O error occurs during token retrieval
-
dummy
Creates a dummy symbol for non-terminal nodes in the parse tree.- Parameters:
nt- the non-terminal symbol- Returns:
- a Symbol representing the non-terminal
-
parseProgram
Parse the entire program.- Returns:
- the parse tree node for Program
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseCode
Parse code block.- Returns:
- the parse tree node for Code
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseInstruction
Parse instruction statement.- Returns:
- the parse tree node for Instruction
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseAssign
Parse assignment statement.- Returns:
- the parse tree node for Assign
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseIf
Parse If statement.- Returns:
- the parse tree node for If
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseOptionalElse
Handles the optional Else branch in an If statement. If the current token is ELSE, it parses the else branch.- Parameters:
node- the If parse tree node to which the else branch will be added- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseWhile
Parse While statement.- Returns:
- the parse tree node for While
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseOutput
Parse Output statement.- Returns:
- the parse tree node for Output
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseInput
Parse Input statement.- Returns:
- the parse tree node for Input
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseCond
Parse condition.- Returns:
- the parse tree node for Cond
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseCondImpl
Parse condition with implication.- Returns:
- the parse tree node for CondImpl
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseCondComp
Parse condition comparison.- Returns:
- the parse tree node for CondComp
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseCondAtom
Parse condition atom.- Returns:
- the parse tree node for CondAtom
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseExprArith
Parse arithmetic expression.- Returns:
- the parse tree node for ExprArith
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseExprAddSub
Parse addition and subtraction.- Returns:
- the parse tree node for ExprAddSub
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseExprMulDiv
Parse multiplication and division.- Returns:
- the parse tree node for ExprMulDiv
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseExprUnary
Parse unary expression.- Returns:
- the parse tree node for ExprUnary
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-
parseExprPrimary
Parse primary expression.- Returns:
- the parse tree node for ExprPrimary
- Throws:
ParseException- if a syntax error occursIOException- if an I/O error occurs
-