�f�������\������K�� ... ���K���@(regular grammar)
���K�\�� (regular expression)�̗�
c = getchar(); while (c == ' ') c = getchar(); switch (c) �p��:{ zzleng = 0; do { zztext[zzleng] = c; zzleng++; c = getchar(); } while (c���p���ł������ł��Ȃ�); backchar(c); zzlex = IDENTIFIER; zzlval = install(zztext,zzleng); } ����: .... .... '+': zzlex = PLUS; ....
%% ' '+ { REJECT } [a-zA-Z][a-zA-Z0-9]* { yylval = install(yytext,yyleng); return(IDENTIFIER); } [0-9]+ { ... } ... [+] { return(PLUS) } .... %%
���@�ɂ��ƂÂ����o�ɂ�鐶�����ł��邩�ǂ����̉��
��͕��@
���o�ł̗�u���v�͍ʼnE���o�B �ō����o�ł����ċA�I�Ȑ����K��������ꍇ�ɂ͉�͂��ł��Ȃ��Ȃ�B ���̏ꍇ�A�\���K�������������čċA�I��͖@���g����悤�� �Ȃ����K�v������B
�� => �� => ��*�� => �W��*�� => ���W*�� => ���W*�� => ���W*�W�� => ���W*(��) => ���W*(��+��) => ���W*(��+��) => ���W*(�W��+��) => ���W*(���W+��) => ���W*(���W+�W��) => ���W*(���W+���W)
�̂悤�ɂȂ�B
�u���v�̍\���K����ύX���A�ċA�I�ȉ�͖@��K�p�����v���O������������B
��̗�ł�procedure E(); begin T(); while token = '+' do begin gettoken(); T() end end; procedure T(); begin F(); while token = '*' do begin gettoken(); F() end end; procedure F(); begin if token = '(' then begin gettoken(); E(); if token <> ')' then error(); end else if token = 'i' then gettoken() else error(); end;
�̂悤�ɁA�\���K�������������Ă���BE -> T, E -> T + T, T -> F, T -> F * F, F -> ( E ), F -> i
L .. ������f��ׂ�
L .. �ō����o
(1) .. 1�̐�ǂ�
derive | X->Y1Y2...Yk | ���o |
error | ��� |
shift | �ǂݍ��� | |
reduce | X->Y1Y2...Yk | �Ҍ� |
accept | �� | |
error | ��� |
repeat s:=�X�^�b�N�ŏ�i�̏��; a:=���ɒ��ׂ悤�Ƃ��Ă���f��; if A[s,a]=shift then begin t:=G[s,a]; �X�^�b�N��a��t�Ƃ�t���ŏ�i�� ����悤�ɉ������݁A���͑f��� ����a����菜�� end else if A[s,a]=reduce X->Y1Y2...Yk then begin �X�^�b�N����k�̏�Ԃ�k�� �L�������o��; u:=�X�^�b�N�ŏ�i�̏��; t:=G[u,X]; �X�^�b�N��X��t�Ƃ�t���ŏ�i�� ����悤�ɉ������� end else if A[s,a]=accept then ��͏I�� else ��� forever;
�u���v�̍\���K���Ƃ��ẮA
��͕\
A | G | |||||||||||||||
��� | + | * | ( | ) | i | $ | + | * | ( | ) | i | $ | E | T | F | |
0 | s | s | 4 | 5 | 1 | 2 | 3 | |||||||||
1 | s | a | 6 | |||||||||||||
2 | 2 | s | 2 | 2 | 7 | |||||||||||
3 | 4 | 4 | 4 | 4 | ||||||||||||
4 | s | s | 4 | 5 | 8 | 2 | 3 | |||||||||
5 | 6 | 6 | 6 | 6 | ||||||||||||
6 | s | s | 4 | 5 | 9 | 3 | ||||||||||
7 | s | s | 4 | 5 | 10 | |||||||||||
8 | s | s | 6 | 11 | ||||||||||||
9 | 1 | s | 1 | 1 | 7 | |||||||||||
10 | 3 | 3 | 3 | 3 | ||||||||||||
11 | 5 | 5 | 5 | 5 |
��͗�
�X�^�b�N | �f��̗� | ���� |
---------------- | ---------- | ------ |
$0 | i*(i+i)$ | shift |
$0i5 | *(i+i)$ | reduce 6 |
$0F3 | *(i+i)$ | reduce 4 |
$0T2 | *(i+i)$ | shift |
$0T2*7 | (i+i)$ | shift |
$0T2*7(4 | i+i)$ | shift |
$0T2*7(4i5 | +i)$ | reduce 6 |
$0T2*7(4F3 | +i)$ | reduce 4 |
$0T2*7(4T2 | +i)$ | reduce 2 |
$0T2*7(4E8 | +i)$ | shift |
$0T2*7(4E8+6 | i)$ | shift |
$0T2*7(4E8+6i5 | )$ | reduce 6 |
$0T2*7(4E8+6F3 | )$ | reduce 4 |
$0T2*7(4E8+6T9 | )$ | reduce 1 |
$0T2*7(4E8 | )$ | shift |
$0T2*7(4E8)11 | $ | reduce 5 |
$0T2*7F10 | $ | reduce 3 |
$0T2 | $ | reduce 2 |
$0E1 | $ | accept |
��̍\���K���� yacc �ŏ������Ayacc -v �R�}���h��p����� y.output �Ƃ����t�@�C�����ł���B