Recursive Descent Parser
This project is a parser created for a madeup language.
-
The following keywords are defined:
○ int
○ string
○ set
○ print
○ println
-
There are four operators:
○ +
○ -
○ *
○ /
-
Grammar rules:
Prog ::= StmtList
StmtList ::= { Stmt T_SC } { StmtList }
Stmt ::= Decl | Set | Print
Decl ::= T_INT T_ID | T_STRING T_ID
Set ::= T_SET T_ID Expr
Print ::= T_PRINT Expr | T_PRINTLN Expr
Expr ::= Term { (T_PLUS|T_MINUS) Expr }
Term ::= Primary { (T_STAR|T_SLASH) Term }
Primary ::= T_ICONST | T_SCONST | T_ID | T_LPAREN Expr T_RPAREN
-
Semantic rules:
- There are only two types: integer and string.
- Variables must be declared before they are used. They are declared with Decl.
- There is one single scope for variables.
- Variable names must be unique; once a name has been declared, it may not be declared again.
- An integer constant is of type integer.
- A string constant is of type string.
- The type of an identifier is the type assigned when it is declared.
- The type of an Expr or a Term is the type of the result of the operation.
- Adding two integers results in an integer representing the sum
- Subtracting two integers results in an integer representing the difference
- Multiplying two integers results in an integer representing the product
- Dividing two integers results in an integer representing the quotient
- Adding two strings results in a string representing the concatenation
- Multiplying an integer by a string results in a string which is the string operand repeated integer times (i.e. 3 * “hi” is “hihihi”)
- Dividing two strings results in an a string where the first instance of the denominator that is found in the numerator is removed.
- ALL OTHER COMBINATIONS OF OPERATIONS ARE UNDEFINED and are an error
- A variable is assigned a value using a Set. The type of the variable being set must match the type of the expression; if it does not this is an error.
- An expression can be printed using either “print” or “println”. In both cases, the value of the Expr is printed to the standard output. In the case of “println”, a newline is printed to standard out at the end of the execution of the program