In my programming class, we are creating a recursive descent parser for a language we came up with. I have a lexer, token, and parser class file with a txt of the code in our made-up programming language. I also have included a grammar analysis file containing our grammar.
private static final String[][] TOKENS =
{
{"PRINT", "\\bPRINT\\b", "PRINT keyword"},
{"LET", "\\bLET\\b", "LET keyword"},
{"OTHIF", "\\bOTHIF\\b", "OTHIF keyword"},
{"OTHERWISE", "\\bOTHERWISE\\b", "OTHERWISE keyword"},
{"ENDIF", "\\bENDIF\\b", "ENDIF keyword"},
{"IF", "\\bIF\\b", "IF keyword"},
{"ENDLOOP", "\\bENDLOOP\\b", "ENDLOOP keyword"},
{"LOOP", "\\bLOOP\\b", "LOOP keyword"},
{"string", "\\\'[^\\\n\\\']*\\\'", "string literal"},
{"number", "0|(\\s\\-)?[1-9][0-9]*", "numeric literal"},
{"identifier", "[a-z][a-zA-Z0-9]*", "identifier"},
{"equalto", "\\=\\=", "is equal to"},
{"assignment", "\\=", "assignment"},
{"add", "\\+", "add/concatenate"},
{"subtract", "\\-", "subtract"},
{"multiply", "\\*", "multiply"},
{"divide", "\\/", "divide"},
{"modulus", "\\bR\\b", "modulus"},
{"parenopen", "\\(", "open parenthesis"},
{"parenclose", "\\)", "close parenthesis"},
{"logicaland", "\\&", "logical and"},
{"logicalor", "\\|", "logical or"},
{"logicalnot", "\\~", "logical not"},
{"less", "\\<", "less than"},
{"greater", "\\>", "greater than"},
{"semicolon", ";", "semicolon"},
{"error", "[^\\s]+", "error"}
};
private void expr()
{
System.out.println("Begin <expr>");
if (next.getLexeme().equals("identifier") || next.getLexeme().equals("number") || next.getLexeme().equals("string"))
{
term();
if (next.getLexeme().equals("add") || next.getLexeme().equals("subtract"))
{
if (next.getLexeme().equals("add"))
{
match("+");
term();
}
else
{
match("-");
term();
}
}
else
{
System.out.println("Syntax error: Unexpected token \"" + next.getLexeme() + "\"");
System.exit(0);
}
}
The grammar for our language is as follows:
========
Kickflip
========
<stmt_list> --> <stmt> { <stmt> }
<stmt> --> <print_stmt> | <let_stmt> | <loop_stmt> | <if_stmt>
<print_stmt> --> PRINT <expr> ;
<let_stmt> --> LET <id> = <expr> ;
<loop_stmt> --> LOOP ( <bool_expr> ) <stmt_list> ENDLOOP
<if_stmt> --> IF ( <bool_expr> ) <stmt_list> { OTHIF ( <bool_expr> ) <stmt_list> } [ OTHERWISE <stmt_list> ] ENDIF
<expr> --> <expr> + <term> | <expr> - <term> | <term>
<term> --> <term> * <factor> | <term> / <factor> | <term> R <factor> | <factor>
<factor> --> ( <expr> ) | <value>
<bool_expr> --> <bool_expr> "|" <bool_term> | <bool_term>
<bool_term> --> <bool_term> & <bool_factor> | <bool_factor>
<bool_factor> --> <value> == <value> | <value> < <value> | <value> > <value> | [~] ( <bool_expr> )
<value> --> <id> | <literal>
<id> --> /*Any identifier recognized by the lexical analyzer*/
<literal> --> /*Any literal recognized by the lexical analyzer*/
and my test file:
PRINT 'Hello!';
When running foldersrc> java Parser.java KickflipTest1.txt in the command line (as required for the assignments testing) I get the following error:
Begin <stmt>
Begin <print_stmt>
Matched PRINT
Begin <expr>
Syntax error: Unexpected token "'Hello!'"
Why is expr() throwing a syntax error? I believe it is because the equals() method is comparing the regular expression of what a string should be (clarified in the String[][] of Lexer.java) instead of if the string is legal within the regular expression. I'm not too savvy with the regex class but any help is appreciated and sorry for the long page.