Syntax error reporting in calc
This commit is contained in:
parent
c26510bdef
commit
a93d4427e2
|
@ -1,20 +1,5 @@
|
|||
parser: LALR(1)
|
||||
terminals:
|
||||
"NUM"
|
||||
"COMMA"
|
||||
"FN"
|
||||
"LPAREN"
|
||||
"RPAREN"
|
||||
"SEMICOLON"
|
||||
"ARROW"
|
||||
"IDENT"
|
||||
"PLUS"
|
||||
"MINUS"
|
||||
"TIMES"
|
||||
"DIVIDE"
|
||||
"EXPONENT"
|
||||
"ASSIGN"
|
||||
|
||||
lexesis: calcLexer.lxs
|
||||
precedence:
|
||||
right "EXPONENT"
|
||||
left "TIMES" "DIVIDE"
|
||||
|
@ -29,10 +14,11 @@ grammar:
|
|||
<toplevel> ::= <toplevel_expr> "SEMICOLON" [toplevel]
|
||||
| <functiondef> "SEMICOLON" [toplevel]
|
||||
| <assign> "SEMICOLON" [toplevel]
|
||||
| <error> "SEMICOLON" [toplevel]
|
||||
| <toplevel_error> "SEMICOLON" [toplevel]
|
||||
;
|
||||
|
||||
<toplevel_expr> ::= <expr> [toplevel];
|
||||
<toplevel_expr> ::= <expr> [toplevel];
|
||||
<toplevel_error> ::= <error> [toplevel];
|
||||
|
||||
<expr> ::= <expr> "PLUS" <expr> [binop]
|
||||
| <expr> "MINUS" <expr> [binop]
|
||||
|
|
|
@ -24,10 +24,15 @@ Parser::Token Parser::lex() {
|
|||
}
|
||||
catch (CalcLexer::NoMatch) {
|
||||
std::cerr << "Syntax error: unrecognized token" << std::endl;
|
||||
m_lex.skip(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Parser::Value Parser::error(Token, const std::vector<calcParser_Symbol>&) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Parser::Value Parser::reduce_toplevel(std::deque<Token> subparts) {
|
||||
if (subparts.empty()) return nullptr;
|
||||
if (subparts[0].symbol == calcParser_Symbol::V_expr) {
|
||||
|
|
|
@ -19,6 +19,7 @@ class Parser : public calcParser<std::unique_ptr<AST>> {
|
|||
protected:
|
||||
using Value = std::unique_ptr<AST>;
|
||||
Token lex() override;
|
||||
Value error(Token current, const std::vector<calcParser_Symbol>& expected) override;
|
||||
Value reduce_arguments(std::deque<Token> subparts) override;
|
||||
Value reduce_assign(std::deque<Token> subparts) override;
|
||||
Value reduce_binop(std::deque<Token> subparts) override;
|
||||
|
|
Loading…
Reference in New Issue