help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [SOLVED with questions] Identifying start of new rule


From: Test User
Subject: Re: [SOLVED with questions] Identifying start of new rule
Date: Sun, 16 Jun 2013 12:17:00 -0500

A new grammar in combination with %glr-parser worked.

%{
#include <stdio.h>
#include <stdlib.h>

 extern  int yylex();
 extern  int yyparse();
 extern  FILE *yyin;

 void yyerror(const char *s);
 %}

%union {
  char cval;
  int ival;
  float fval;
  char *sval;
}

/* define the "terminal symbol" token types */
...
%start syntax
%glr-parser
%%
syntax:
| syntax rule { printf("Success!\n"); }

rule:  LEFT_ANGLEB rule_name RIGHT_ANGLEB { printf("found LHS rule
name\n"); }EXPANDS_TO expression ;

expression     : list | list OR expression ;

list    : term | term list ;

term    : literal | LEFT_ANGLEB rule_name RIGHT_ANGLEB  ;


rule_name: IDENTIFIER ;

      /* actually, the original BNF did not use quotes */
literal : LITERAL_STRING | QUOTED_STRING ;

%%

This grammar has 1 shift/reduce conflict, as opposed to 6 in the
previous grammar, and %glr-parser works this time.

Question:

(1)I understand that if I have a file of the form:
<lhs1> ::= <rhs1> <lhs2> ::= <rhs2>, bison parses my rules by default
as (<lhs1> ::= <rhs1> <lhs2>) ::= <rhs2> and generates a syntax error
when it reaches the ::= before <rhs2>.  In Bison terminology (I hope),
after recognising <rhs1>, I want Bison to reduce <lhs1> ::= <rhs1> to
<rule>, but the next token '<' does not give it a reason to do so. It would
have to skip '<', 'lhs2', '>' and finally arrive at '::=' to know that
(<lhs1> ::= <rhs1> <lhs2>) will not work. Therefore, as written, a 2-token
lookahead as suggested by Ron Burk would not help me. Am I
understanding correctly?

However, if I defined a token RULE_NAME as, for example
\< [A-Za-z_]+\>
then after matching the longest possible sequence of tokens in:
NAME ::= NAME ...<end-of-rule> NAME ::= <whatever> so that I am at
<end-of-rule>, where I want to reduce, then a 2-token lookahead *would*
help in this case. Is that right?

Regards,
Test User.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]