a yacc problem, help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • betterking@gmail.com

    a yacc problem, help!

    I want to write a tiny sql parser with lex and yacc. At first I just
    write sql.l sql.y with simple CREATE TABLE funciton.

    sql.l

    %{
    #include "y.tab.h" //<>
    #include <stdlib.h>
    #include <string.h>
    void yyerror(char *);
    %}

    %%
    CREATE { return CREATE;}

    TABLE { return TABLE;}


    [0-9]+ {
    yylval.intval =
    atoi(yytext);
    return INTEGER;
    }

    [a-z][a-z0-9]* {
    strcpy(yylval.n ameval,
    yytext);
    // fprintf(stderr,
    "-----%s\n", *yytext);
    return NAME;
    }

    [<>=] {
    yylval.compval =
    yytext[0];
    return COMPARE;
    }

    [\n] { return *yytext;
    }

    [ \t] ;

    .. {
    yyerror("Unknow n
    Character!");
    }
    %%

    int yywrap(void)
    {
    return 1;
    }


    sql.y:

    %{
    #include <stdio.h>
    void yyerror(char *s);
    int yylex(void);

    %}

    %union {
    int intval;
    char nameval[50];
    char compval;
    };

    %token <intvalINTEGE R
    %token <namevalNAME
    %token <compvalCOMPA RE
    %token CREATE TABLE

    %%
    createtablestat ement:
    | createtablestat ement '\n'
    | CREATE TABLE NAME '\n' {printf("%s\n", $3);}
    ;

    %%
    void yyerror(char *s)
    {
    fprintf(stderr, " %s\n", s);
    }

    int main(void)
    {
    yyparse();
    }


    I compile and run it as follows:

    lex sql.l
    yacc -d sql.y
    gcc -o create lex.yy.c y.tab.c -ll

    $./create
    CREATE TABLE aaaa
    aaaa
    CREATE TABLE bbbb
    parse error
    $

    I want to know why it could run at the first time, but it could not
    run at the second time.
    Please help me. Thanks a lot!
  • Nick Keighley

    #2
    Re: a yacc problem, help!

    On 4 Oct, 03:10, "betterk...@gma il.com" <betterk...@gma il.comwrote:
    I want to write a tiny sql parser with lex and yacc. At first I just
    write sql.l sql.y with simple CREATE TABLE funciton.
    <snip>

    both yacc and sql are off-topic in comp.lang.c.
    You need to try a compiler or unix ng for questions
    about yacc.


    --
    Nick Keighley

    Comment

    • Dann Corbit

      #3
      Re: a yacc problem, help!

      <betterking@gma il.comwrote in message
      news:d4919413-2c09-49ae-b990-70bc75a7cd8a@x4 1g2000hsb.googl egroups.com...
      >I want to write a tiny sql parser with lex and yacc. At first I just
      write sql.l sql.y with simple CREATE TABLE funciton.
      >
      sql.l
      >
      %{
      #include "y.tab.h" //<>
      #include <stdlib.h>
      #include <string.h>
      void yyerror(char *);
      %}
      >
      %%
      CREATE { return CREATE;}
      >
      TABLE { return TABLE;}
      >
      >
      [0-9]+ {
      yylval.intval =
      atoi(yytext);
      return INTEGER;
      }
      >
      [a-z][a-z0-9]* {
      strcpy(yylval.n ameval,
      yytext);
      // fprintf(stderr,
      "-----%s\n", *yytext);
      return NAME;
      }
      >
      [<>=] {
      yylval.compval =
      yytext[0];
      return COMPARE;
      }
      >
      [\n] { return *yytext;
      }
      >
      [ \t] ;
      >
      . {
      yyerror("Unknow n
      Character!");
      }
      %%
      >
      int yywrap(void)
      {
      return 1;
      }
      >
      >
      sql.y:
      >
      %{
      #include <stdio.h>
      void yyerror(char *s);
      int yylex(void);
      >
      %}
      >
      %union {
      int intval;
      char nameval[50];
      char compval;
      };
      >
      %token <intvalINTEGE R
      %token <namevalNAME
      %token <compvalCOMPA RE
      %token CREATE TABLE
      >
      %%
      createtablestat ement:
      | createtablestat ement '\n'
      | CREATE TABLE NAME '\n' {printf("%s\n", $3);}
      ;
      >
      %%
      void yyerror(char *s)
      {
      fprintf(stderr, " %s\n", s);
      }
      >
      int main(void)
      {
      yyparse();
      }
      >
      >
      I compile and run it as follows:
      >
      lex sql.l
      yacc -d sql.y
      gcc -o create lex.yy.c y.tab.c -ll
      >
      $./create
      CREATE TABLE aaaa
      aaaa
      CREATE TABLE bbbb
      parse error
      $
      >
      I want to know why it could run at the first time, but it could not
      run at the second time.
      You want news:comp.compi lers
      This may be helpful:


      ** Posted from http://www.teranews.com **

      Comment

      Working...