include stdio h int yylexvoid void yyerrorchar token

  • Slides: 19
Download presentation

例 %{ #include <stdio. h> int yylex(void); void yyerror(char *); %} %token INTEGER %%

例 %{ #include <stdio. h> int yylex(void); void yyerror(char *); %} %token INTEGER %% program: program expr 'n’ { printf( "%dn”, $2 ); } | ; expr: INTEGER | expr '+’ expr { $$ = $1 + $3; } | expr '-’ expr{ $$ = $1 - $3; } ; %% void yyerror( char *s ) { fprintf( stderr, "%sn”, s ); } int main( void ) { yyparse( ); return 0; } 言語プロセッサ 2016(東京 科大学CS学部) 8

cal 1. tab. h ( bison –d cal 1. y で生成) #ifndef YYTOKENTYPE #define

cal 1. tab. h ( bison –d cal 1. y で生成) #ifndef YYTOKENTYPE #define YYTOKENTYPE /* Put the tokens into the symbol table, so that GDB and other debuggers know about them. */ enum yytokentype { INTEGER = 258 }; #endif /* Tokens. */ #define INTEGER 258 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef int YYSTYPE; #define yystype YYSTYPE /* obsolescent; will be withdrawn */ #define YYSTYPE_IS_DECLARED 1 #define YYSTYPE_IS_TRIVIAL 1 #endif extern YYSTYPE yylval; 9 言語プロセッサ 2016(東京 科大学CS学部)

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

lexer. l %{ #include "y. tab. h" #include <stdlib. h> void yyerror(char *); %} %% [0 -9]+ { yylval = atoi(yytext); return INTEGER; } [-+n] { return( *yytext); } [ t] { /* skip whitespace */ }. { yyerror( "Unknown character” ); } %% int yywrap( void ) { return 1; } 言語プロセッサ 2016(東京 科大学CS学部) 10

%{ #include <stdio. h> /* Printf */ #include <stdlib. h> /* exit */ #define

%{ #include <stdio. h> /* Printf */ #include <stdlib. h> /* exit */ #define YYSTYPE int yyparse( void ); int yylex( void ); void yyerror( char *mes ); %} %token number %token QUIT 254 %%. . . → next page %% int main() { printf("Enter expression with + - * / ( ) n"); yyparse(); return 0; } void yyerror( char *mes ) { fprintf( stderr, “%sn”, mes); } 言語プロセッサ 2016(東京 科大学CS学部) 12

program : program expr ‘n’ | ; expr : expr '+' term | expr

program : program expr ‘n’ | ; expr : expr '+' term | expr '-' term | QUIT ; term : term '*’ fact | term '/’ fact | fact ; fact : number | '-’ number | '(’ expr ')' ; { printf( “= %dn”, $2 ); } { { puts( "expr 1” ); $$ = $1 + $3; } puts( "expr 2” ); $$ = $1 - $3; } puts( "expr 3” ); $$ = $1; } exit( 0 ); } { puts( "term 1” ); $$ = $1 * $3; } { puts( "term 2” ); $$ = $1 / $3; } { puts( "term 3” ); $$ = $1; } { puts( "fact 1” ); $$ = -$2; } { puts( "fact 2” ); $$ = $2; } 言語プロセッサ 2016(東京 科大学CS学部) 13

cal 2. tab. h ( bison –d cal 2. y ) /* Tokens. */

cal 2. tab. h ( bison –d cal 2. y ) /* Tokens. */ #ifndef YYTOKENTYPE #define YYTOKENTYPE /* Put the tokens into the symbol table, so that GDB and other debuggers know about them. */ enum yytokentype { number = 258, QUIT = 254 }; #endif /* Tokens. */ #define number 258 #define QUIT 254 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef int YYSTYPE; #define yystype YYSTYPE /* obsolescent; will be withdrawn */ #define YYSTYPE_IS_DECLARED 1 #define YYSTYPE_IS_TRIVIAL 1 #endif extern YYSTYPE yylval; 言語プロセッサ 2016(東京 科大学CS学部) 14

構文図の例(1) factor = ident | number | "(" expression ")". factor ident number (

構文図の例(1) factor = ident | number | "(" expression ")". factor ident number ( expression 言語プロセッサ 2016(東京 科大学CS学部) ) 17

練習 構文図で書いてみよう ! PL/0言語の構文規則 program = block ". ". block = [ "CONST" ident

練習 構文図で書いてみよう ! PL/0言語の構文規則 program = block ". ". block = [ "CONST" ident "=" number { ", " ident "=" number } "; " ] [ "VAR" ident { ", " ident } "; " ] { "PROCEDURE" ident "; " block "; " } statement = [ ident ": =" expression | "CALL" ident | "? " ident | "!" expression | "BEGIN" statement { "; " statement } "END" | "IF" condition "THEN" statement | "WHILE" condition "DO" statement ]. condition = "ODD" expression | expression ( "=" | "#" | "<=" | ">=" ) expression = [ "+" | "-" ] term { ( "+" | "-" ) term }. term = factor { ( "*" | "/" ) factor }. factor = ident | number | "(" expression ")". 言語プロセッサ 2016(東京 科大学CS学部) 19