| acmsguru |
|---|
| Finished |
Program Parser;
Procedure Skip; Forward;
Function Peek:Char; Forward;
Procedure Error; Forward;
<parsing routines forward-declarations>
<parsing routines>
Var
St:String;
Pos:Integer;
Procedure Error;
Begin
WriteLn('NO');
Halt;
End;
Procedure Skip;
Begin
Inc(Pos);
If Pos>Length(St) Then Error;
End;
Function Peek:Char;
Begin
Peek:=St[Pos];
End;
Begin
ReadLn(St);
St:=St+'#';
Pos:=1;
Parse;
If Pos=Length(St) Then WriteLn('YES') Else WriteLn('NO');
End.
<parsing routines forward-declarations> contains a definition for each of the parsing functions, followed by Forward;. The part labeled <parsing routines> contains a definition followed by a body for each of the parsing routines.
Procedure <name>;
Skip, Peek or Error). A body of a parsing routine is:
Begin
<segment>
<segment>
...
<segment>
End;
<name>;, where <name> is a name of some parsing routine.
If Peek=<character> Then Begin
Skip;
<segment>
<segment>
...
<segment>
End Else If Peek=<character> Then Begin
Skip;
<segment>
<segment>
...
<segment>
End Else If Peek=<character> Then Begin
...
End Else If Peek=<character> Then Begin
End;
<segment> is again either unconditional or conditional. The simplest form of a conditional segment has only one If and no Else. Note that we always skip a character after guessing it right, and that's the only case where we invoke Skip;. Each <character> is a non-apostrophe character with ASCII code between 33 and 126 wrapped in apostrophes, like 'a'. It is not necessary to have at least one <segment> after Skip; in the If body. The last End; can also be written as End Else Error;, meaning that all other peek outcomes are unacceptable. Parse. Peek=... conditions) way of evaluating all If's in the body of some routine that do not result in a call to Error;, there should be one production rule concatenating the corresponding nonterminals and terminals (see example for further clarification).Parse.'AB' instead of 'A''B'. Your output should contain exactly n lines, where n is the number of parsing routines in the input.
Program Parser;
Procedure Skip; Forward;
Function Peek:Char; Forward;
Procedure Error; Forward;
Procedure Parse; Forward;
Procedure Addend; Forward;
Procedure Term; Forward;
Procedure Number; Forward;
Procedure Term;
Begin
If Peek='0' Then Begin
Skip;
Number;
End Else If Peek='1' Then Begin
Skip;
Number;
End Else If Peek='(' Then Begin
Skip;
Parse;
If Peek=')' Then Begin
Skip;
End Else Error;
End Else If Peek='P' Then Begin
Skip;
If Peek='I' Then Begin
Skip;
End Else If Peek='E' Then Begin
Skip;
End Else Error;
End Else Error;
End;
Procedure Number;
Begin
If Peek='0' Then Begin
Skip;
Number;
End Else If Peek='1' Then Begin
Skip;
Number;
End;
End;
Procedure Addend;
Begin
Term;
If Peek='*' Then Begin
Skip;
Addend;
End Else If Peek='/' Then Begin
Skip;
Addend;
End;
End;
Procedure Parse;
Begin
Addend;
If Peek='+' Then Begin
Skip;
Parse;
End Else If Peek='-' Then Begin
Skip;
Parse;
End;
End;
Var
St:String;
Pos:Integer;
Procedure Error;
Begin
WriteLn('NO');
Halt;
End;
Procedure Skip;
Begin
Inc(Pos);
If Pos>Length(St) Then Error;
End;
Function Peek:Char;
Begin
Peek:=St[Pos];
End;
Begin
ReadLn(St);
St:=St+'#';
Pos:=1;
Parse;
If Pos=Length(St) Then WriteLn('YES') Else WriteLn('NO');
End.
<addend>::=<term>|<term>'*'<addend>|<term>'/'<addend>
<number>::=|'0'<number>|'1'<number>
<parse>::=<addend>|<addend>'+'<parse>|<addend>'-'<parse>
<term>::='('<parse>')'|'0'<number>|'1'<number>|'PE'|'PI'
'PE' and 'PI'. | Author: | Petr Mitrichev |
| Resource: | Petr Mitrichev Contest 3 |
| Date: | September 01, 2007 |
| Name |
|---|


