public
Description: An implementation of markdown in C, using a PEG grammar
Clone URL: git://github.com/jgm/peg-markdown.git
100644 47 lines (36 sloc) 0.8 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
%{
#include <stdio.h>
int vars[26];
%}
 
Stmt  = - e:Expr EOL      { printf("%d\n", e); }
  | ( !EOL . )* EOL    { printf("error\n"); }
 
Expr  = i:ID ASSIGN s:Sum    { $$= vars[i]= s; }
  | s:Sum        { $$= s; }
 
Sum  = l:Product
    ( PLUS r:Product  { l += r; }
    | MINUS r:Product  { l -= r; }
    )*      { $$= l; }
 
Product  = l:Value
    ( TIMES r:Value  { l *= r; }
    | DIVIDE r:Value  { l /= r; }
    )*      { $$= l; }
 
Value  = i:NUMBER      { $$= atoi(yytext); }
  | i:ID !ASSIGN      { $$= vars[i]; }
  | OPEN i:Expr CLOSE    { $$= i; }
 
NUMBER  = < [0-9]+ >  -    { $$= atoi(yytext); }
ID  = < [a-z] >  -    { $$= yytext[0] - 'a'; }
ASSIGN  = '='    -
PLUS  = '+'    -
MINUS  = '-'    -
TIMES  = '*'    -
DIVIDE  = '/'    -
OPEN  = '('    -
CLOSE  = ')'    -
 
-  = [ \t]*
EOL  = '\n' | '\r\n' | '\r' | ';'
 
%%
 
int main()
{
  while (yyparse());
 
  return 0;
}