d: put YYEMPTY in the TokenKind

* data/skeletons/d.m4, data/skeletons/lalr1.d (b4_token_enums): Rename
YYTokenType as TokenKind.
Define YYEMPTY.
* examples/d/calc.y, tests/calc.at, tests/scanner.at: Adjust.
This commit is contained in:
Akim Demaille
2020-04-13 08:45:46 +02:00
parent 3877b7210e
commit 71e3f6d4da
7 changed files with 39 additions and 39 deletions

7
TODO
View File

@@ -120,6 +120,11 @@ https://www.cs.tufts.edu/~nr/cs257/archive/clinton-jefferey/lr-error-messages.pd
https://research.swtch.com/yyerror https://research.swtch.com/yyerror
http://gallium.inria.fr/~fpottier/publis/fpottier-reachability-cc2016.pdf http://gallium.inria.fr/~fpottier/publis/fpottier-reachability-cc2016.pdf
* D
** yylex
It would be better to have TokenKind as return value. Can we use reflexion
to support both output types?
* Modernization * Modernization
Fix data/skeletons/yacc.c so that it defines YYPTRDIFF_T properly for modern Fix data/skeletons/yacc.c so that it defines YYPTRDIFF_T properly for modern
and older C++ compilers. Currently the code defaults to defining it to and older C++ compilers. Currently the code defaults to defining it to
@@ -264,7 +269,7 @@ It would be a very nice source of inspiration for the other languages.
Valentin Tolmer is working on this. Valentin Tolmer is working on this.
** yychar == yyempty_ ** yychar == YYEMPTY
The code in yyerrlab reads: The code in yyerrlab reads:
if (yychar <= YYEOF) if (yychar <= YYEOF)

View File

@@ -27,7 +27,7 @@ public interface Lexer
* to the next token and prepares to return the semantic value * to the next token and prepares to return the semantic value
* and beginning/ending positions of the token. * and beginning/ending positions of the token.
* @return the token identifier corresponding to the next token. */ * @return the token identifier corresponding to the next token. */
YYTokenType yylex (); TokenKind yylex ();
/** /**
* Entry point for error reporting. Emits an error * Entry point for error reporting. Emits an error
@@ -39,7 +39,7 @@ public interface Lexer
void yyerror (YYLocation loc, string s); void yyerror (YYLocation loc, string s);
} }
- semantic types are handled by D usions (same as for C/C++ parsers) - semantic types are handled by D unions (same as for C/C++ parsers)
- the following (non-standard) %defines are supported: - the following (non-standard) %defines are supported:

View File

@@ -164,11 +164,9 @@ m4_define([b4_token_enum],
# Output the definition of the tokens as enums. # Output the definition of the tokens as enums.
m4_define([b4_token_enums], m4_define([b4_token_enums],
[/* Token kinds. */ [/* Token kinds. */
public enum YYTokenType { public enum TokenKind {
]b4_symbol_kind([-2])[ = -2,
/** Token returned by the scanner to signal the end of its input. */ b4_symbol_foreach([b4_token_enum])dnl
EOF = 0,
b4_symbol_foreach([b4_token_enum])
} }
]) ])

View File

@@ -429,7 +429,7 @@ b4_locations_if([, ref ]b4_location_type[ yylocationp])[)
public bool parse () public bool parse ()
{ {
/// Lookahead and lookahead in internal form. /// Lookahead and lookahead in internal form.
int yychar = yyempty_; int yychar = TokenKind.YYEMPTY;
SymbolKind yytoken = SymbolKind.]b4_symbol_prefix[YYEMPTY; SymbolKind yytoken = SymbolKind.]b4_symbol_prefix[YYEMPTY;
/* State. */ /* State. */
@@ -493,7 +493,7 @@ m4_popdef([b4_at_dollar])])dnl
} }
/* Read a lookahead token. */ /* Read a lookahead token. */
if (yychar == yyempty_) if (yychar == TokenKind.YYEMPTY)
{]b4_parse_trace_if([[ {]b4_parse_trace_if([[
yycdebugln ("Reading a token");]])[ yycdebugln ("Reading a token");]])[
yychar = yylex ();]b4_locations_if([[ yychar = yylex ();]b4_locations_if([[
@@ -532,7 +532,7 @@ m4_popdef([b4_at_dollar])])dnl
yy_symbol_print ("Shifting", yytoken, yylval]b4_locations_if([, yylloc])[);]])[ yy_symbol_print ("Shifting", yytoken, yylval]b4_locations_if([, yylloc])[);]])[
/* Discard the token being shifted. */ /* Discard the token being shifted. */
yychar = yyempty_; yychar = TokenKind.YYEMPTY;
/* Count tokens shifted since error; after three, turn off error /* Count tokens shifted since error; after three, turn off error
* status. */ * status. */
@@ -573,7 +573,7 @@ m4_popdef([b4_at_dollar])])dnl
if (yyerrstatus_ == 0) if (yyerrstatus_ == 0)
{ {
++yynerrs_; ++yynerrs_;
if (yychar == yyempty_) if (yychar == TokenKind.YYEMPTY)
yytoken = SymbolKind.]b4_symbol_prefix[YYEMPTY; yytoken = SymbolKind.]b4_symbol_prefix[YYEMPTY;
yyerror (]b4_locations_if([yylloc, ])[yysyntax_error (yystate, yytoken)); yyerror (]b4_locations_if([yylloc, ])[yysyntax_error (yystate, yytoken));
} }
@@ -584,14 +584,14 @@ m4_popdef([b4_at_dollar])])dnl
/* If just tried and failed to reuse lookahead token after an /* If just tried and failed to reuse lookahead token after an
* error, discard it. */ * error, discard it. */
if (yychar <= YYTokenType.EOF) if (yychar <= TokenKind.]b4_symbol(0, [id])[)
{ {
/* Return failure if at end of input. */ /* Return failure if at end of input. */
if (yychar == YYTokenType.EOF) if (yychar == TokenKind.]b4_symbol(0, [id])[)
return false; return false;
} }
else else
yychar = yyempty_; yychar = TokenKind.YYEMPTY;
} }
/* Else will try to reuse lookahead token after shifting the error /* Else will try to reuse lookahead token after shifting the error
@@ -841,7 +841,6 @@ m4_popdef([b4_at_dollar])])dnl
private static immutable int yylast_ = ]b4_last[; private static immutable int yylast_ = ]b4_last[;
private static immutable int yynnts_ = ]b4_nterms_number[; private static immutable int yynnts_ = ]b4_nterms_number[;
private static immutable int yyempty_ = -2;
private static immutable int yyfinal_ = ]b4_final_state_number[; private static immutable int yyfinal_ = ]b4_final_state_number[;
private static immutable int yyntokens_ = ]b4_tokens_number[; private static immutable int yyntokens_ = ]b4_tokens_number[;

View File

@@ -99,16 +99,15 @@ class CalcLexer(R) : Lexer
while (!input.empty && input.front != '\n' && isWhite (input.front)) while (!input.empty && input.front != '\n' && isWhite (input.front))
input.popFront; input.popFront;
// Handle EOF.
if (input.empty) if (input.empty)
return YYTokenType.EOF; return TokenKind.YYEOF;
// Numbers. // Numbers.
if (input.front.isNumber) if (input.front.isNumber)
{ {
import std.conv : parse; import std.conv : parse;
semanticVal_.ival = input.parse!int; semanticVal_.ival = input.parse!int;
return YYTokenType.NUM; return TokenKind.NUM;
} }
// Individual characters // Individual characters
@@ -116,16 +115,15 @@ class CalcLexer(R) : Lexer
input.popFront; input.popFront;
switch (ch) switch (ch)
{ {
case EOF: return YYTokenType.EOF; case '=': return TokenKind.EQ;
case '=': return YYTokenType.EQ; case '+': return TokenKind.PLUS;
case '+': return YYTokenType.PLUS; case '-': return TokenKind.MINUS;
case '-': return YYTokenType.MINUS; case '*': return TokenKind.STAR;
case '*': return YYTokenType.STAR; case '/': return TokenKind.SLASH;
case '/': return YYTokenType.SLASH; case '(': return TokenKind.LPAR;
case '(': return YYTokenType.LPAR; case ')': return TokenKind.RPAR;
case ')': return YYTokenType.RPAR; case '\n': return TokenKind.EOL;
case '\n': return YYTokenType.EOL; default: assert(0);
default: assert(0);
} }
} }
} }

View File

@@ -306,13 +306,13 @@ class CalcLexer(R) : Lexer
// Handle EOF. // Handle EOF.
if (input.empty) if (input.empty)
return YYTokenType.EOF; return TokenKind.CALC_EOF;
// Numbers. // Numbers.
if (input.front.isNumber) if (input.front.isNumber)
{ {
semanticVal_.ival = parseInt; semanticVal_.ival = parseInt;
return YYTokenType.NUM; return TokenKind.NUM;
} }
// Individual characters // Individual characters

View File

@@ -126,7 +126,7 @@ class YYLexer(R) : Lexer
import std.uni : isNumber; import std.uni : isNumber;
// Handle EOF. // Handle EOF.
if (input.empty) if (input.empty)
return YYTokenType.EOF; return TokenKind.END;
auto c = input.front; auto c = input.front;
input.popFront; input.popFront;
@@ -136,13 +136,13 @@ class YYLexer(R) : Lexer
{ {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
semanticVal_.val = c - '0'; semanticVal_.val = c - '0';
return YYTokenType.NUM; return TokenKind.NUM;
case '+': return YYTokenType.PLUS; case '+': return TokenKind.PLUS;
case '-': return YYTokenType.MINUS; case '-': return TokenKind.MINUS;
case '*': return YYTokenType.STAR; case '*': return TokenKind.STAR;
case '/': return YYTokenType.SLASH; case '/': return TokenKind.SLASH;
case '(': return YYTokenType.LPAR; case '(': return TokenKind.LPAR;
case ')': return YYTokenType.RPAR; case ')': return TokenKind.RPAR;
default: assert(0); default: assert(0);
} }
} }