/media/sda-magnetic/david/Extern-Magnetic-2022-06-29/Extern01/Dokumente-2020-11-16/disk10-ab-2020-01-10/02-debian-pc2-work/informatik/yacc/yacc/yacc.txt


LALR(1)-Parser

parser.y    ->      Yacc            -> y.tab.c
y.tab.c     ->      C-Compiler      -> a.out
Eingabe     ->      a.out           -> Ausgabe

Spezifikationsfile: parser.y

yacc parser.y
gcc y.tab.c -ly

-ly!!! DAmit Yacc-Bibliotheken eingebunden werden. 

Der erzeugte Parser steht als Funktion yyparse() zur Verfügung

Yacc-Spezifikation


Deklarationen
%%
Grammatik
%%
Hilfsprozeduren




%{
#include <ctype.h>
#include <stdio.h>
%}
%token NUMBER
%%
lines           : lines expr '\n' {printf("%d\n", $2); }
                | lines '\n'
                |
                ;
expr            : expr '+' term { $$ = $1 + $3; }
                | expr '-' term { $$ = $1 - $3; }
                | term
                |;
term            : term '*' factor { $$ = $1 * $3; }
                | term '/' factor { $$ = $1 * $3; }
                | factor
                ;
factor          : '(' expr ')' { $$ = $2; }
                | NUMBER
                ;
%%
int yylex() {
    int c;
    c = getchar();
    if (isdigit(c)) {
        ungetc(c, stdin); 
        scanf("%d", &yylval);
        return NUMBER;
    }
    return c;
}


Yacc-Spezifikationen, Grammatik-Teil:

- Nichtterminale (lines, expr, term, ...)
- Terminale (Token): 
    - Einzelne Zeichen, Typ char oder int '+', '(', '\n'
    - Explizit deklarierte Token (NUMBER); Im Deklarationsteil 
      %token NUMBER
- Produktionen