0

this is part of y.ouput file

state 65

15 Expression: Expression . "&&" Expression

16 | Expression . "<" Expression

17 | Expression . "+" Expression

18 | Expression . "-" Expression

19 | Expression . "*" Expression

20 | Expression . "[" Expression "]"

21 | Expression . "." "length"

22 | Expression . "." Identifier "(" Expression "," Expression ")"

25 | "!" Expression .

"["  shift, and go to state 67
"<"  shift, and go to state 69
"+"  shift, and go to state 70
"-"  shift, and go to state 71
"*"  shift, and go to state 72
"."  shift, and go to state 73

"["       [reduce using rule 25 (Expression)]
"<"       [reduce using rule 25 (Expression)]
"+"       [reduce using rule 25 (Expression)]
"-"       [reduce using rule 25 (Expression)]
"*"       [reduce using rule 25 (Expression)]
"."       [reduce using rule 25 (Expression)]
$default  reduce using rule 25 (Expression)

this is how the precedence of operators is set

%left "&&"

%left '<'

%left '-' '+' 

%left '*'

%right '!'

%left '.'

%left '(' ')'

%left '[' ']'
Gourav Saha
  • 43
  • 3
  • 10

1 Answers1

0

In bison, there is a difference between "x" and 'x'; they are not the same token. So, assuming you are using bison, your precedence declarations don't refer to the terminals in the productions.

Bison also allows %token definitions of the following form:

%token name quoted-string ...

For example (a short excerpt from bison's own grammar file):

%token
  PERCENT_CODE            "%code"
  PERCENT_DEBUG           "%debug"
  PERCENT_DEFAULT_PREC    "%default-prec"
  PERCENT_DEFINE          "%define"
  PERCENT_DEFINES         "%defines"
  PERCENT_ERROR_VERBOSE   "%error-verbose"

Once the symbols have been aliased, they can be used interchangeably in the grammar, making it possible to use the double-quoted string in productions; some people find such grammars easier to read. However, there is no mechanism to ensure that the lexer produces the correct token number for a double-quoted string since it only has access to the token names.

The "original" yacc, at least in the current "byacc" version maintained by Thomas Dickey, allows both single- and double-quoted token names, but does not distinguish between them; both "+" and '+' are mapped to token number 43 ('+'). It also does not provide any easy way to alias token names, so the double-quoted multi-character strings are not particularly easy to use in a reliable way.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Generally you NEVER want to use `"`-quoted tokens, as there is no easy way to get your lexer to return them properly to your parser. – Chris Dodd Sep 05 '13 at 17:17
  • @ChrisDodd: unless you alias them to a token name (`%token AND_TOKEN "&&"`), which arguably makes the grammar more readable. – rici Sep 05 '13 at 17:24
  • @rici: that doesn't alias them -- it just declares two tokens. In order to alias them, you need to alias them explicitly to the same fixed integer: `%token AND_TOKEN "&&" 512`, which can be done but is error prone. – Chris Dodd Sep 05 '13 at 17:41
  • @ChrisDodd: http://www.gnu.org/software/bison/manual/html_node/Token-Decl.html#Token-Decl "Once you equate the literal string and the token name, you can use them interchangeably in further declarations or the grammar rules." (By the way, it would be `%token AND_TOKEN 512 "&&"`, but that usage is indeed error-prone.) – rici Sep 05 '13 at 17:51
  • @rici: interesting -- that would seem to be a bison extension -- 'normal' yacc doesn't work that way. – Chris Dodd Sep 05 '13 at 17:56
  • @ChrisDodd: I tried to summarize comments in answer, in case it becomes useful to someone else. – rici Sep 05 '13 at 19:00