r/Compilers • u/Potato871 • Sep 05 '26
Disambiguating keyword-like identifiers using operators
I was answering a post about another language which had inflicted a lot of verbosity upon itself by requiring every variable be referenced as Variable[name] to distinguish from keywords.
This brought me to a thought I wanted to share, about a potential way to deal with this.
Before the token stream has been assembled into the AST, you could run a pass over it where each operator token checks it's adjacent neighbours and if they resolved as keywords that shouldn't normally resolve to values, turn them back into plain identifiers.
Now this is just a very basic concept I wanted to float here, to get some push back, and edge cases, because I find the concept interesting.
5
u/jcastroarnaud Sep 05 '26
I use a simpler procedure. While lexing, pick keywords as if they were common identifiers; then, match each identifier token against the keyword list, and change the token type to "keyword" if there's a match. Then, the token list can be passed along to the parser.
3
u/4xe1 Sep 06 '26 edited Sep 06 '26
What about parentheses? knowing where you are and how that affect whether you may be an id or a keyword is generally not a local information in the token stream. Being "right next to an operator in the token stream" is only an incomplete and dirty proxi for it. This info is best known when you already have the AST
An other option, if you need to know whether tokens are keywords upon building the AST, and need the AST upon deciding whether tokens are keywords, is to mix the timing of tokenization and AST build:
3
u/rjmarten Sep 05 '26
Okay, so that might work for
if + 1. But what about1 + if? In most languages, keywords/reserved words are the first token of a valid expression/statement. So in those cases, you would have to check what token comes after the if/continue/fn/case/etc.And if your parser is doing that, it means you are forcing human readers to do the same thing. A better solution, IMHO, would be a prefix, like
$. Then you can parse$if + 1or1 + $ifif you really feel the need. An underscore suffix works just as well, and usually doesn't require additional complexity in the lexer/parser.3
u/EggplantExtra4946 Sep 05 '26
If you call it with a enable_keywords=true and it encounters
whileit returns the id of the while keyword, otherwise it returns the id of an identifier token. Usually you would call it with enable_keywords=true but in contexts where you allow any identifiers (this supposes a recursive descent parser), even keywords, you call it with enable_keywords=false, for example for struct fields. You might even use flags for enabling different sets of keywords if you need more granularity.2
u/johnwcowan Sep 06 '26
In PL/I there are no reserved words: 'if if = then then then = else else = if;" is a traditional example of a weird but valid statement. But every statement type except assignments begins with a keyword (even procedure calls), and assignments are always statements, never expressions (there are no expressions with side effects). The traditional aporoaches are ad hoc parsing and having a lookahead routine between the lexer and the parser that detects assignment statements and inserts a dummy keyword in front if them. However, I believe a PEG parser will also work, although I have yet to write a grammar.
One irritation is that many PL/I statements contain components, each beginning with its own keyword, that can appear in any order but only once per statement (and some are mutually exclusive). Trying to handle this in the rules causes a combinatorial explosion. I intend to have rules containing "(component1|component2|component3)*", which allows any components in any order, and then do post-parsing checks of the syntax tree for uniqueness and incompatibility.