1. Dashboard
  2. Forum
    1. Unerledigte Themen
  3. Mitglieder
    1. Letzte Aktivitäten
    2. Benutzer online
    3. Team-Mitglieder
    4. Trophäen
    5. Mitgliedersuche
  4. Tutorial Bereich
  • Anmelden
  • Registrieren
  • Suche
Dieses Thema
  • Alles
  • Dieses Thema
  • Dieses Forum
  • Seiten
  • Forum
  • Lexikon
  • Erweiterte Suche
  1. Informatik Forum
  2. Webmaster & Internet
  3. Entwicklung

Flex & Yacc compiler error

  • fleksik
  • 29. August 2006 um 13:26
  • Unerledigt
  • fleksik
    2
    fleksik
    Mitglied
    Punkte
    15
    Beiträge
    2
    • 29. August 2006 um 13:26
    • #1

    I can't compile my flex and yacc code because of following errors...

    flex konw.lex
    bison -v -d konw.y -o konw.c
    gcc -g konw.c lex.yy.c -lfl -o konw
    lex.yy.c: In function `yy_get_next_buffer':
    lex.yy.c:996: error: syntax error before ';' token
    lex.yy.c:996: error: syntax error before ')' token
    lex.yy.c:1056: error: syntax error before ';' token
    lex.yy.c:1056: error: syntax error before ')' token
    lex.yy.c: In function `yy_get_previous_state':
    lex.yy.c:1100: error: syntax error before ';' token
    lex.yy.c:1100: error: syntax error before ')' token
    lex.yy.c:1108: error: `yy_c' undeclared (first use in this function)
    lex.yy.c:1108: error: (Each undeclared identifier is reported only once
    lex.yy.c:1108: error: for each function it appears in.)
    lex.yy.c: At top level:
    lex.yy.c:1117: error: syntax error before "return"
    lex.yy.c: In function `yy_scan_string':
    lex.yy.c:1493: error: syntax error before ';' token
    lex.yy.c:1493: error: syntax error before ')' token
    lex.yy.c: In function `yy_scan_bytes':
    lex.yy.c:1521: error: syntax error before ';' token
    lex.yy.c:1521: error: syntax error before ')' token
    mingw32-make.exe: *** [konw] Error 1

    My source code:

    FLEX
    ====

    %{
    #include "konw.h"
    #include <stdio.h>
    #include <string.h>
    %}
    %x koment dekl_int dekl_char for for1 for2 for3 for4
    ZMIENNA [a-zA-Z][a-zA-Z0-9]+
    LICZBA [0-9]+
    %%
    Flex-RegEx
    "/*" BEGIN(koment);
    <koment>"*/" BEGIN(INITIAL);
    <koment>.
    <koment>\n
    int { BEGIN(dekl_int); return L_INT; }
    <dekl_int>[ \t]* /* ignoruj biale spacje */
    <dekl_int>{ZMIENNA} return L_ID;
    <dekl_int>";" { BEGIN(INITIAL); return L_SREDNIK; }
    char { BEGIN(dekl_char); return L_CHAR; }
    <dekl_char>[ \t]* /* ignoruj biale spacje */
    <dekl_char>{ZMIENNA} return L_ID;
    <dekl_char>";" { BEGIN(INITIAL); return L_SREDNIK; }
    for[ \t]* { BEGIN(for); return L_FOR; }
    <for>"(" { BEGIN(for1); return L_LEWY; }
    <for1>[ \t]* /* ignoruj biale spacje */
    <for1>[^';']* printf("%s",yytext);
    <for1>";" { BEGIN(for2); return L_SREDNIK; }
    <for2>";" { BEGIN(for3); return L_SREDNIK; }
    <for2>[ \t]* /* ignoruj biale spacje */
    <for2>[^';']* { printf("%s",yytext); BEGIN(for3); }
    <for3>")" { BEGIN(for4); return L_PRAWY; }
    <for3>[ \t]* /* ignoruj biale spacje */
    <for3>[^')']* printf("%s",yytext);
    <for4>";" { BEGIN(INITIAL); return L_SREDNIK; }
    <for4>[ \t]* /* ignoruj biale spacje */
    <for4>. printf("%c",yytext[0]);
    . return yytext[0];
    %%
    int yywrap(void){
    return 1;
    }

    YACC
    ====

    %{
    #include <stdio.h>
    int yylex(void);
    %}
    %token L_FOR L_LEWY L_PRAWY L_SREDNIK L_INT L_CHAR L_ID L_NUM L_FUNKCJA
    %start program
    %%
    program
    : kod
    ;
    kod
    : instrukcja
    | kod instrukcja
    ;

    deklaracja
    : L_INT L_ID
    | L_CHAR L_ID
    ;
    petla_for
    : L_FOR L_LEWY wyrazenie L_SREDNIK wyrazenie L_SREDNIK wyrazenie L_PRAWY instrukcja
    ;
    operator
    : '/'
    | '='
    | '!='
    | '<'
    | '>'
    ;
    wyrazenie
    : L_ID operator L_ID
    | L_ID operator L_NUM
    ;
    funkcja
    : L_FUNKCJA L_LEWY deklaracja L_PRAWY
    ;
    instrukcja
    : L_SREDNIK
    | deklaracja L_SREDNIK
    | petla_for
    | wyrazenie
    | funkcja
    ;
    %%
    void yyerror(char *s){
    fprintf(stderr, "%s\n", s);
    exit(2);
    }
    int main(void){
    yyparse();
    return 0;
    }

    What is wrong ?

    Wojtek

  • Paulchen
    1
    Paulchen
    Gast
    • 29. August 2006 um 14:35
    • #2
    Zitat von fleksik

    for[ \t]* { BEGIN(for); return L_FOR; }
    <for>"(" { BEGIN(for1); return L_LEWY; }

    "for" is a keyword in the C language. Because of this you may not use "for" as the name of a token in your scanner. Replace "for" with something different, and your program will compile.

  • fleksik
    2
    fleksik
    Mitglied
    Punkte
    15
    Beiträge
    2
    • 30. August 2006 um 22:08
    • #3
    Zitat von Paulchen

    "for" is a keyword in the C language. Because of this you may not use "for" as the name of a token in your scanner. Replace "for" with something different, and your program will compile.



    I have changed all wrong tokens and this is the result:

    flex konw.lex
    bison -v -d konw.y -o konw.c
    gcc -g konw.c lex.yy.c -lfl -o konw

    The proper code:

    FLEX
    ====

    %{
    #include "konw.h"
    #include <stdio.h>
    #include <string.h>
    %}
    %x koment dekl_liczba dekl_znak petla petla1 petla2 petla3 petla4
    ZMIENNA [a-zA-Z][a-zA-Z0-9]+
    LICZBA [0-9]+
    %%
    Flex-RegEx
    "/*" BEGIN(koment);
    <koment>"*/" BEGIN(INITIAL);
    <koment>.
    <koment>\n
    int { BEGIN(dekl_liczba); return L_LICZBA; }
    <dekl_liczba>[ \t]* /* ignoruj biale spacje */
    <dekl_liczba>{ZMIENNA} return L_ID;
    <dekl_liczba>";" { BEGIN(INITIAL); return L_SREDNIK; }
    char { BEGIN(dekl_znak); return L_ZNAK; }
    <dekl_znak>[ \t]* /* ignoruj biale spacje */
    <dekl_znak>{ZMIENNA} return L_ID;
    <dekl_znak>";" { BEGIN(INITIAL); return L_SREDNIK; }
    for[ \t]* { BEGIN(petla); return L_PETLA; }
    <petla>"(" { BEGIN(petla1); return L_LEWY; }
    <petla1>[ \t]* /* ignoruj biale spacje */
    <petla1>[^';']* printf("%s",yytext);
    <petla1>";" { BEGIN(petla2); return L_SREDNIK; }
    <petla2>";" { BEGIN(petla3); return L_SREDNIK; }
    <petla2>[ \t]* /* ignoruj biale spacje */
    <petla2>[^';']* { printf("%s",yytext); BEGIN(petla3); }
    <petla3>")" { BEGIN(petla4); return L_PRAWY; }
    <petla3>[ \t]* /* ignoruj biale spacje */
    <petla3>[^')']* printf("%s",yytext);
    <petla4>";" { BEGIN(INITIAL); return L_SREDNIK; }
    <petla4>[ \t]* /* ignoruj biale spacje */
    <petla4>. printf("%c",yytext[0]);
    . return yytext[0];
    %%

    YACC
    ====

    %{
    #include <stdio.h>
    int yyerror( char *s );
    %}
    %token L_PETLA L_LEWY L_PRAWY L_SREDNIK L_LICZBA L_ZNAK L_ID L_NUM L_FUNKCJA
    %start program
    %%
    program
    : kod
    ;
    kod
    : instrukcja
    | kod instrukcja
    ;

    deklaracja
    : L_LICZBA L_ID
    | L_ZNAK L_ID
    ;
    petla
    : L_PETLA L_LEWY wyrazenie L_SREDNIK wyrazenie L_SREDNIK wyrazenie L_PRAWY instrukcja
    ;
    operator
    : '/'
    | '='
    | '!='
    | '<'
    | '>'
    ;
    wyrazenie
    : L_ID operator L_ID
    | L_ID operator L_NUM
    ;
    funkcja
    : L_FUNKCJA L_LEWY deklaracja L_PRAWY
    ;
    instrukcja
    : L_SREDNIK
    | deklaracja L_SREDNIK
    | petla
    | wyrazenie
    | funkcja
    ;
    %%
    int yyerror(char* s)
    {
    fprintf (stderr, "%s", s);
    }

    Regards,
    Wojtek

  • Maximilian Rupp 27. Dezember 2024 um 12:05

    Hat das Thema aus dem Forum Programmieren nach Entwicklung verschoben.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!

Benutzerkonto erstellen Anmelden

Rechtliches

Impressum

Datenschutzerklärung