Irony: How to give KeyTerm precedence over variable?
Asked Answered
T

2

13

Relevant chunk of Irony grammar:

var VARIABLE = new RegexBasedTerminal("variable", @"(?-i)\$?\w+");

variable.Rule = VARIABLE;
tag_blk.Rule = html_tag_kw + attr_args_opt + block;
term_simple.Rule = NUMBER | STRING | variable | boolean | "null" | term_list;
term.Rule = term_simple | term_filter;
block.Rule = statement_list | statement | ";";
statement.Rule = tag_blk | directive_blk | term;

The problem is that both a "tag" and a "variable" can appear in the same place. I want my parser to prefer the tag over the variable, but it always prefers the variable. How can I change that?

I've tried changing tag_blk.Rule to PreferShiftHere() + html_tag_kw + attr_args_opt + block; and ImplyPrecedenceHere(-100) + html_tag_kw + attr_args_opt + block; but it doesn't help any. The parser doesn't even complain of an ambiguity.

Tapdance answered 11/4, 2011 at 1:58 Comment(2)
Which lexer and parser generator are you using?Dint
@Kaleb: Irony. I put it as a tag, but I guess I should have made it more evident.Tapdance
E
2

Try changing the order of 'tag_blk.Rule' and 'variable.Rule' as tokenisers usually go after first match, and variable is first in your list.

Eagleeyed answered 18/4, 2011 at 21:8 Comment(1)
I'd understand if they were within the same rule (A | B vs B | A) but...I'd be shocked if this made a difference. Nevertheless, I'll try it...Tapdance
C
1

You can increase the Priority of the tag_blk Terminal or decrease the one of variable whichever suits your purpose. Terminal class has a Priority field defaulting to 0. According to the comment right above it

// Priority is used when more than one terminal may match the input char. 
// It determines the order in which terminals will try to match input for a given char in the input.
// For a given input char the scanner uses the hash table to look up the collection of terminals that may match this input symbol. 
// It is the order in this collection that is determined by Priority property - the higher the priority, 
// the earlier the terminal gets a chance to check the input.

Unfortunately I can't test this at the moment as the code fragment provided needs work and lots of assumptions to be made compilable. But from the description above this should be the one you are looking for. Hope it helps someone -even 10 years after the question aired.

Czechoslovak answered 13/8, 2021 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.