Created
September 9, 2013 20:54
-
-
Save smithcommajoseph/6501364 to your computer and use it in GitHub Desktop.
Jison version of PragProg's (the book) timerparser from chapt 2 exercise 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* lexical grammar */ | |
%lex | |
%% | |
\s+ /* skip whitespace */ | |
([0-9]) return 'DIGIT' | |
"am" return 'AM' | |
"pm" return 'PM' | |
":" return ':' | |
<<EOF>> return 'EOF' | |
/lex | |
%start time | |
%% /* language grammar */ | |
time : spec EOF | |
{ if ($1 >= 24*60) console.log("Time is too large"); | |
console.log("%d minutes past midnight", $1); | |
return; | |
} | |
; | |
spec : hour ':' minute | |
{ $$ = $1 + $3; } | |
| hour ':' minute ampm | |
{ if ($1 > 11*60) console.log("Hour out of range"); | |
$$ = $1 + $3 + $4; | |
} | |
| hour ampm | |
{ if ($1 > 11*60) console.log("Hour out of range"); | |
$$ = $1 + $2; | |
} | |
; | |
hour : hour_num | |
{ if ($1 > 23) console.log("Hour out of range"); | |
$$ = $1 * 60; | |
} | |
; | |
minute : DIGIT DIGIT | |
{ $$ = $1 * 10 + Number($2); | |
if ($$ > 59) console.log("Minute out of range"); | |
} | |
; | |
ampm : AM | |
{ $$ = 0; } | |
| PM | |
{ $$ = 12 * 60; } | |
; | |
hour_num : DIGIT | |
{ $$ = $1; } | |
| DIGIT DIGIT | |
{ $$ = $1 * 10 + Number($2); } | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment