0

Hi I am trying to follow the book "Modern Compiler Implementation in C" in the 3 chapter PARSER I am not able execute the parser exercise.

%{
#include <stdio.h>
#include "util.h"
#include "errormsg.h"

int yylex(void); /* function prototype */ 

void yyerror(char *s)
{
EM_error(EM_tokPos, "%s", s);
}
%}


%union {
    int pos;
    int ival;
    string sval;
    }

%token <sval> ID STRING
%token <ival> INT

%token
    LET

%start program

%%

program:  LET

I run this against the test file which contains

test1.tig

 let

I am getting

test1.tig:1.1: syntax error

Parsing failed

Link for the Exercises provided online Chap3

I am not able to find why the error is occuring even for just a word .

  1. Is the exercise in the parser correct in the link above ?
  2. Could you help me to run the parser successfully ? I have written rest of the code too.
Community
  • 1
  • 1
KARTHIK BHAT
  • 1,410
  • 13
  • 23
  • 1
    How does your yylex() work? Have you tested it? – n. m. could be an AI Sep 07 '15 at 17:22
  • yes it was working when i tested it previous example where i implemented lexer. i copied the same lex.yy.c file to the later chapter. Is there anyway to check if it is working wrt parser ? – KARTHIK BHAT Sep 07 '15 at 17:35
  • 1
    The split line `%token` with `LET` on the next is puzzling. Is that actually allowed/recognized/compiled, or is it a typo in the question? Have you proved that the lexer is returning LET for the input? How have you done that? Have you compiled with the parser debugging enabled — so it reports the tokens it gets and the actions it takes? `-DYYDEBUG=1` and `yydebug = 1;`, IIRC. – Jonathan Leffler Sep 07 '15 at 17:44
  • 1
    @JonathanLeffler: bison, at least, treats newline as just another whitespace character in most contexts, including `%`-directives. That seems to be Posix standard; under "Lexical structure of the grammar", it says "The ``, ``, and `` character shall be ignored..." – rici Sep 07 '15 at 20:32
  • @rici: OK; yup. It's legitimate to put `%token` and a token name on different lines according to the POSIX [`yacc`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/yacc.html) specification. I wouldn't allow it in my local style rules, but that's a separate matter. The rest of the comment still applies — how do we know the lexer is returning the correct value; indeed, what tokens is it returning. – Jonathan Leffler Sep 07 '15 at 20:39
  • @JonathanLeffler: Yes, that's the important point. It's quite possible that the lexer and the parser don't agree about token values. (As for the split `%token` lines, bison itself uses [this style](http://git.savannah.gnu.org/cgit/bison.git/tree/src/parse-gram.y#n139), and I've been known to do so, too -- but without useless semicolon. But it's a question of personal taste.) – rici Sep 07 '15 at 20:45
  • If the input file really starts with a space character, it could be that your lexer is not correctly ignoring whitespace. Or it could be any of the problems mentioned by JonathanLeffler. – rici Sep 07 '15 at 20:57

0 Answers0