yacc.dvi

Similar documents
exec.dvi

lexex.dvi

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

untitled

(ver. 1.3 (2018) ) yacc 1 1 yacc yacc (Yet Another Compiler Compiler) UNIX yacc yacc y *.y yacc ) yacc *.tab.h *.tab.c C C yacc yacc UNIX yacc bison 2

untitled

C言語によるアルゴリズムとデータ構造

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

double float

ex01.dvi

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

r07.dvi

compiler-text.dvi

ohp07.dvi

1.

() () (parse tree) ( (( ) * 50) ) ( ( NUM 10 + NUM 30 ) * NUM 50 ) ( * ) ( + ) NUM 50 NUM NUM (abstract syntax tree, AST) ( (( ) * 5

( ) ( ) lex LL(1) LL(1)

:30 12:00 I. I VI II. III. IV. a d V. VI

ex01.dvi

ohp03.dvi

:30 12:00 I. I VI II. III. IV. a d V. VI

橡Pro PDF

() / (front end) (back end) (phase) (pass) 1 2

C

変数を使えるようにする arithr.y を拡張して変数を使えるようにする 変数はアルファベット小文字一文字だけからなるものとする 変数の数はたかだか 26 なので,26 個の要素をもつ配列 vbltable に格納する 一行だけで計算が終わるのではなく数式を連続で計算できるようにし,$ が入力され

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

r03.dvi

新版明解C言語 実践編

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

卒 業 研 究 報 告.PDF

Microsoft Word - C.....u.K...doc

18/12/06 情報工学実験 C コンパイラ (2018 年度 ) 担当 : 笹倉 佐藤 その 3 yacc の構造 定義部 %% 定義部の終了 規則部 %% 規則部の終了 ユーザ定義サブルーチン部 :C のプログラムを書く 形は lex と同じ 1

Taro-リストⅢ(公開版).jtd

XMPによる並列化実装2

1.ppt

情報工学実験 C コンパイラ第 2 回説明資料 (2018 年度 ) 担当 : 笹倉 佐藤

programmingII2019-v01

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

r08.dvi

Microsoft PowerPoint - kougi9.ppt

£Ã¥×¥í¥°¥é¥ß¥ó¥°(2018) - Âè11²ó – ½ÉÂꣲ¤Î²òÀ⡤±é½¬£² –

超初心者用

ohp08.dvi

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

第5回お試しアカウント付き並列プログラミング講習会

新・明解C言語 実践編

untitled

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

Condition DAQ condition condition 2 3 XML key value

main

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

gcc C C tcc lex yacc flex bison [ ] Tiny C 2 Lex [ 2.6 ] 2.1 lex yacc [ ] lex flex yacc bison yacc yyparse() C yyparse() yylex() yylex() yylex() flex

P05.ppt

明解Java入門編

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

新・明解C言語 ポインタ完全攻略

Microsoft Word - Cプログラミング演習(12)

I117 II I117 PROGRAMMING PRACTICE II 2 SOFTWARE DEVELOPMENT ENV. 2 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu

ex12.dvi

file:///D|/C言語の擬似クラス.txt

1 (2 * 3) 1 2 * 3 Preorder In order Post order 1 * 1 * Breadth-first Depth-first * * 3 Preorder: 1 * 2 3 In order: 1 2 * 3 Post orde

tuat1.dvi

cpp1.dvi

WinHPC ppt

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

program.dvi

‚æ4›ñ

LIFO(last in first out, ) 1 FIFO(first in first out, ) 2 2 PUSH POP : 1

Java演習(4) -- 変数と型 --

Taro-リストⅠ(公開版).jtd

slide5.pptx

C B

新コンフィギュレータのフレームワークについて

情報工学実験 C コンパイラ第 2 回説明資料 (2017 年度 ) 担当 : 笹倉 佐藤

スライド タイトルなし

SystemC言語概論

プログラミング及び演習 第1回 講義概容・実行制御

新・明解C言語で学ぶアルゴリズムとデータ構造

1.3 ( ) ( ) C

Jacques Garrigue

1) OOP 2) ( ) 3.2) printf Number3-2.cpp #include <stdio.h> class Number Number(); // ~Number(); // void setnumber(float n); float getnumber();


double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

joho07-1.ppt

6-1

: gettoken(1) module P = Printf exception End_of_system (* *) let _ISTREAM = ref stdin let ch = ref ( ) let read () = (let c =!ch in ch := inp

kiso2-03.key

Krylov (b) x k+1 := x k + α k p k (c) r k+1 := r k α k Ap k ( := b Ax k+1 ) (d) β k := r k r k 2 2 (e) : r k 2 / r 0 2 < ε R (f) p k+1 :=

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

A/B (2018/06/08) Ver kurino/2018/soft/soft.html A/B

ohp07.dvi

PowerPoint Presentation

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

Transcription:

2017 c 8 Yacc Mini-C C/C++, yacc, Mini-C, run,, Mini-C 81 Yacc Yacc, 1, 2 ( ), while ::= "while" "(" ")" while yacc 1: st while : lex KW WHILE lex LPAREN expression lex RPAREN statement 2: 3: $$ = new St while($3, $5); 4: 1, st while, while lex KW WHILE, while lex LPAREN, (() expression, lex RPAREN, ()) statement, while 2 4, $1 while $2 $3 $4 $5 cond St while body "while" "(" ")" expression statement Yacc, while 5, $1, $2,, $5, $3, $5, while, $3 $5, 3 $$ St while, while 8 1

, run, 82 Yacc 1 Makefile parseyy yacc t822bmc, factormc 2 parseyy yacc 7 ( Part-1 Part-7 ) Part-1 ( % % ), yacc C/C++ #include "asth" #include <cstdio> C++ C stdioh // lex extern int linenum; extern std::file* yyin; int yylex(void); 1 void yyerror(const char*); Part-2 (%union ) lex, INT lex CHAR int yylvalval, lex ID char* string, 2 while, Part-3 (%token ) %token <val> lex INT lex CHAR %token <string> lex ID, (val string Part-2 ) (, ), Part-4 (%type ) Part-3 ( ), Part-4 ( ) ( ) Part-5 (%start ) %start program program Part-6 ( ) 8 2

, Part-7 ( ),, / main main, (argv[1]) Mini-C, 7 yyparse(); int main(int argc, char *argv[]) if(std::file* fp = std::fopen(argv[1], "r")) linenum = 1; // lex 1 yyin = fp; yyparse(); else // lex // printf(" %s \n", argv[1]); 3 yacc yacc bison --debug -d parseyy -o parsecpp, parsecpp parsecpph (linux)/parsehpp (Cygwin) lex 4 lexll lexll A #include "lexh", lex yacc, yacc, lexh,, yacc, asth Cygwin #include "asth" #include "parsehpp" Linux #include "asth" #include "parsecpph" 5 Makefile 8 3

make mci mci 1, Mini-C /mci factormc,, 83 Yacc 81 20 ::= "(" ")",, 20 ::= 1 parseyy Part-2 expression %union char* string; int val; Expression* expression; 2 Part-4, expression4 // -------------------------------------------------------------------- // [Part-4] // -------------------------------------------------------------------- %type <expression> expression4 3 Part-6 expression4 // -------------------------------------------------------------------- // [Part-6] // -------------------------------------------------------------------- program : expression4 : lex INT $$ = new Exp constant(type INT, $1); 1 Mini-C Interpreter 8 4

::= $1 lex INT 2 Exp constant, $$, 4, program // -------------------------------------------------------------------- // [Part-6] // -------------------------------------------------------------------- program : expression4 $1->print(std::cout); std::cout<<std::endl; expression4 : lex INT $$ = new Exp constant(type INT, $1);, 1 ::= 20 ::= program, $1, expression4 print 5 6 make mci t801mc (lex ) 2004 /mci t801mc, ( 2004) 82 20 ::= 8 5

1 expression4, expression4 : lex INT $$ = new Exp constant(type INT, $1); lex CHAR 2 $$ = new Exp constant(type CHAR, $1); 6 t802mc x, x OK /mci t802mc 83 20 ::= 1 expression4 expression4 : lex INT $$ = new Exp constant(type INT, $1); lex CHAR $$ = new Exp constant(type CHAR, $1); exp variable $$ = $1; exp variable, 11 ::=, Part-6 8 6

exp variable : lex ID $$ = new Exp variable($1); $1 lex ID, Exp variable 2 exp variable Part-2 %union exp variale exp variable %union char* string; int val; Expression* expression; Exp variable* exp variable; Part-4 exp variale %union exp variale // -------------------------------------------------------------------- // [Part-4] // -------------------------------------------------------------------- %type <expression> expression4 %type <exp variable> exp variable 3 t803mc (lex ) xyz OK t801mc, t802mc 84, 19 ::= ( "*" "/" "%" ) 19 ::= "*",, "*", "*" "*",, 1 expression3 8 7

expression3 : expression4 $$ = $1; expression3 lex STAR expression4 $$ = new Exp operation2(operator MUL, $1, $3); 2, program program : expression3 $1->print(std::cout); std::cout<<std::endl; 3 Part-4 expression3 4 %type <expression> expression4 %type <exp variable> exp variable %type <expression> expression3 t804mc 123 * x *2 mci ((123 * x) * 2) OK t801mc t803mc 85 1 expression3,, yacc,, 19 ::= ( "*" "/" "%" ) 19 ::= "*" "/" "%" 2,, t805mc, mci 23 * x / 45 % c 8 8

(((23 * x) / 45) % c) OK 86 +, 18 ::= ( "+" "-" ) ( "+" "-" ) 18 ::= ( "+" ), (+ -) 1 expression2 expression2 : expression3 $$ = $1; lex PLUS expression3 $$ = new Exp operation1(operator PLUS, $2); 2, program program : expression2 $1->print(std::cout); std::cout<<std::endl; 3 Part-4 expression2 4 %type <expression> expression4 %type <exp variable> exp variable %type <expression> expression3 %type <expression> expression2 + t806mc, mci + 81 / a * 23 (+((81 / a) * 23)) OK 87 8 9

18 ::= ( "+" "-" ) 1 t807mc, mci -123*x (-(123 * x)) OK t805mc, t806mc 88 18 ::= ( "+" "-" ) ( "+" "-" ), 1 t808mc, mci -123 + a*48-9/xy (((-123) + (a * 48)) - (9 / xy)) OK 89 expression, 17 ::= ( "<" ">" "<=" ">=" "==" "!=" ) program Part-4 86 1 t809mc, mci -23*a == b!= 2+9 < x > y <= z >= -2 (((((((-(23 * a)) == b)!= (2 + 9)) < x) > y) <= z) >= (-2)) OK 810,, 20 ::= "(" ")" 1 expression4??? 8 10

??? expression??? 2 $$ = $2; t810mc, mci (10-(x+y)*8) > a/(-2) ((10 - ((x + y) * 8)) > (a / (-2))) OK 811, 20 ::= "(" ")" 1 expression4, exp function $$ = $1; 2 exp function, 21 ::= "(" ")",??? exp function :?????? explist??? $$ = new Exp function($1, *$3); delete $3; explist, expression list 4 $3 * $3, Exp function list 2,, delete 3 explist, 16 ::= ",", 2 C,, 8 11

explist : $$ = new std::list<expression*>; // expression $$ = new std::list<expression*>; $$->push back($1); explist lex COMMA expression $1->push back($3); $$ = $1;, 4 Part-2 Part-4, exp function explist Part-2 1 std::list<expression*>* explist; Part-4 2 5 %type <expression> exp function %type <explist> explist t811mc, mci asum(15,a+3)*9==5 ((asum(15,(a + 3)) * 9) == 5) OK, t801mc t810mc 812, 9 ::= "" "" if while return statement, Statement 1 Part-6 8 12

statement : st assign $$ = $1; lex LBRACE st list lex RBRACE $$ = $2; st if $$ = $1; st while $$ = $1; st return $$ = $1; st function $$ = $1; st assign, st assign : st list : st if : st while : st return : st function :,, program : statement $1->print(std::cout); std::cout<<std::endl; 2 Part-4 %type <statement> statement %type <statement> st assign %type <statement> st if %type <statement> st while %type <statement> st return %type <statement> st function %type <statement> st list 3 Part-2 Statement* statement; 4 make mci Statement* statement; parseyy contains 7 useless nonterminals and 26 useless rules parseyy contains 1 reduce/reduce conflict,,, Segmentation fault 813 1 Part-6 10 ::= "=" ";" st assign??? 8 13

st assign : exp variable????????? 2 $$ = new St assign($1, $3); t813mc, mci x = (a*b+c)/d ; x = (((a * b) + c) / d); OK 814 ( ) 8 :: = 1 Part-6 st list stlist (stlist Part-2, 4 ) stlist st list, stlist (list<statement*>, st list (St list) st list : stlist $$ = new St list(*$1); delete $1; stlist : $$ = new std::list<statement*>; stlist statement $1->push back($2); $$ = $1; 2 t814mc, mci x = a+2; y = b*c; z = x+y; 8 14

, 815 if x = (a + 2); y = (b * c); z = (x + y); OK if 12 if ::= "if" "(" ")" "if" "(" ")" "else" 1 Part-6????????? st if : lex KW IF lex LPAREN expression lex RPAREN statement $$ = new St if($3, $5, NULL);????????? 2 $$ =?????????; t815amc, mci, if (a>0) b = a; else b = -a; if ((a > 0)) b = a; else b = (-a); OK t815bmc, mci, if (a>0) b = a; if ((a > 0)) b = a; else OK 8 15

816 while 1 while 2 13 while ::= "while" "(" ")" t816mc, mci, while (i<n) s = s * i; i = i + 1; while ((i < n)) s = (s * i); i = (i + 1); OK 817 return 1 return 2 14 return ::= "return" ";" t817mc, mci return 9; OK 818 1 15 ::= "(" ")" ";" 811 explist, 2 t818mc, mci gcd(25,b);, gcd(25,b); OK 819 3 ::= 4 ::= "int" "char" 8 16

1 Part-6 variable dcl, type variable dcl : type lex ID $$ = new Variable($1, $2); type : lex KW INT $$ = Type INT; lex KW CHAR $$ = Type CHAR; 2 Part-2 Part-4, variable type Part-2 1 Variable* variable; Type type; Part-4 2 %type <variable> variable dcl %type <type> type 3 Part-6 program program : variable dcl 4 $1->print(std::cout); std::cout<<std::endl; t819mc, mci int x OK 820 ( ) 1, 5 ::= "(" ")" "" "" 6 :: = "," 7 :: = ";" 5 ::= "(" ")" "" "" 8 17

Part-6 function dcl, argument dcllist, variable dcllist, st list??? function dcl :????????? argument dcllist?????? variable dcllist st list??? $$ = new Function($1, $2, *$4, *$7, $8); delete $4; delete $7; 2, 6 :: = ",",, 6 :: =, Part-6 3 argument dcllist : $$ = new std::list<variable*>; 7 :: = ";", 7 :: =, Part-6 variable dcllist : $$ = new std::list<variable*>; 4 Part-2 Part-4, function dcl, argument dcllist, variable dcllist Part-2 Function* function; std::list<variable*>* varlist; Part-4 %type <function> function dcl %type <varlist> argument dcllist %type <varlist> variable dcllist 5 Part-6 program 8 18

program : function dcl 6 $1->print(std::cout); std::cout<<std::endl; t820mc, mci int kansuu() a = 10; return x + y; int kansuu() a = 10; return (x + y); OK 821 ( ) 1 Part-6 argument dcllist 6 :: = ","??? argument dcllist : $$ = new std::list<variable*>;??? $$ = new std::list<variable*>; $$->push back($1);????????? 2 $1->push back($3); $$ = $1; t821amc, mci 8 19

int kansuu(int n) OK t821bmc, mci int kansuu(int n, char c, int x) OK 822 ( ) 1 Part-6 variable dcllist 7 :: = ";" 2 t822amc mci int kansuu(int n, char c, int x) int i; int j; int k; return x; OK t822bmc mci int asum(int n) int s; int i; s = 0; i = -n; while(i<=n) if (i<0) s = s - i; else s = s + i; i = i + 1; return s; 8 20

(, ) OK 823, 1 ::= 2 ::= ( ";" ) 1 Part-6 program program : function dcl $1->print(std::cout); std::cout<<std::endl; program : dcllist ast = build program($1); (program) (dcllist) program, yacc, ( ) $$, ast,,, build program ( ) 2 Part-6 (dcllist) 2 ::= ( ";" ), (variable dcl) (function dcl), Declaration data, (vars) (funcs) Declaration data vars funcs 8 21

dcllist : $$ = new Declaration data; dcllist variable dcl lex SEMICOLON $1->varspush back($2); $$ = $1; dcllist function dcl $1->funcspush back($2); $$ = $1; 3 Declaration data, asth asth Return data, struct Declaration data ; std::list<variable*> vars; std::list<function*> funcs; Varible, map Function Variable #include <map> class Function; class Variable; 4 Part-1 ast build program int yylex(void); int yyerror(char*); Program* ast; Program* build program(declaration data* dd); 5 Part-2 Part-4, dcllist Part-2 Declaration data* declaration data; Part-4 %type <declaration data> dcllist 6 Part-7 build program "main", 8 22

Program* build program(declaration data* dd) std::list<function*> flist; Function* mainf; // dd funcs, "main" // mainf, flist std::list<function*>::iterator f; for(f = dd->funcsbegin(); f!= dd->funcsend(); f++) if ((*f)->name() == "main") mainf = *f; else flistpush back(*f); return new Program(dd->vars, flist, mainf); 7 Part-7 main ast int main(int argc, char *argv[]) if(std::file* fp = std::fopen(argv[1], "r")) linenum = 1; // lex 1 yyin = fp; yyparse(); ast->print(std::cout); else 8 // lex // printf(" %s \n", argv[1]); mini-c factormc mci 8 23

1: char separator; 2: 3: 4: int factor(int n) 5: 6: int d; 7: d = 2; 8: while (((d * d) <= n)) 9: if (((n % d) == 0)) 10: putint(d); 11: putchar(separator); 12: n = (n / d); 13: 14: else 15: d = (d + 1); 16: 17: 18: putint(n); 19: putchar( \n ); 20: 21: 22: 23: int main() 24: 25: int n; 26: putchar( n ); 27: putchar( = ); 28: n = getint(); 29: separator = * ; 30: factor(n); 31: OK 824, 1 Part-7 main ast->print(std::cout);, run ast->run(); 2 factormc mci, 825 mini-c,, 8 24

825 mini-c (8 25mc) parseyy lexll asth astcpp Nagisa ISHIURA Mini-C 1 ::= 2 ::= ( ";" ) 3 ::= 4 ::= "int" "char" 5 ::= "(" ")" "" "" 6 :: = "," 7 :: = ";" 8 :: = 9 ::= "" "" if while return 10 ::= "=" ";" 11 ::= 12 if ::= "if" "(" ")" "if" "(" ")" "else" 13 while ::= "while" "(" ")" 14 return ::= "return" ";" 15 ::= "(" ")" ";" 16 ::= "," 17 ::= ( "<" ">" "<=" ">=" "==" "!=" ) 18 ::= ( "+" "-" ) ( "+" "-" ) 19 ::= ( "*" "/" "%" ) 20 ::= "(" ")" 21 ::= "(" ")" 8 25