Dynamic PEG for interpreted languages.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

130 lignes
2.2 KiB

%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
;
enum op { String, Query, Star, Plus } ;
typedef union Node Node;
struct String { enum op type; char *string; };
struct Query { enum op type; Node *children[1]; };
struct Star { enum op type; Node *children[1]; };
struct Plus { enum op type; Node *children[1]; };
union Node {
enum op type;
struct String String;
struct Query Query;
struct Star Star;
struct Plus Plus;
};
Node *mkString(char *s)
{
Node *node= calloc(1, sizeof(struct String));
node->type= String;
node->String.string= strdup(s);
return node;
}
Node *mkQuery(Node *n)
{
Node *node= calloc(1, sizeof(struct Query));
node->type= Query;
node->Query.children[0]= n;
return node;
}
Node *mkStar(Node *n)
{
Node *node= calloc(1, sizeof(struct Star));
node->type= Star;
node->Star.children[0]= n;
return node;
}
Node *mkPlus(Node *n)
{
Node *node= calloc(1, sizeof(struct Plus));
node->type= Plus;
node->Plus.children[0]= n;
return node;
}
void print(Node *node)
{
switch (node->type) {
case String:
printf("\"%s\"", node->String.string);
return;
case Query:
print(node->Query.children[0]);
printf("?");
return;
case Star:
print(node->Query.children[0]);
printf("*");
return;
case Plus:
print(node->Query.children[0]);
printf("+");
return;
}
abort();
}
void println(Node *node)
{
print(node);
printf("\n");
}
#define YYSTYPE Node *
YYSTYPE yylval = 0;
%}
start = - o:or { yylval = o }
or = a:and "|" - o:or { $$ = mkOr(o, a) }
| a:and { $$ = a }
and = p:postfix a:and { $$ = mkAnd(p, a) }
| p:postfix { $$ = p }
postfix = s:atom ( "?" - { s = mkQuery(s) }
| "*" - { s = mkStar(s) }
| "+" - { s = mkPlus(s) }
)? { $$ = s}
atom = string | class
string = '"' < [^"]* > '"' { $$ = mkString(yytext) } -
class = '[' ... ']'
- = space*
space = [ \t] | '\n' '\r'* | '\r' '\n'*
%%
int main(int argc, char **argv)
{
while (yyparse()) {
println(yylval);
}
return 0;
(void)yySet;
(void)yyPop;
(void)yyPush;
(void)yyAccept;
(void)yymatchDot;
(void)yymatchString;
(void)yymatchChar;
}