-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelloworld.py
executable file
·51 lines (34 loc) · 1.49 KB
/
helloworld.py
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
48
49
50
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from bnf.group import Group
from bnf.literal import Literal
# syntaxe 0
language0 = Group(["Hello", "World!"]) # language0 ::= "Hello" "World!"
print "language0:", language0
# syntaxe 1
class Hello(Literal): # Hello ::= "Hello"
__token__ = "Hello"
class World(Literal): # World ::= "World!"
__token__ = "World!"
language1 = Group([Hello, World]) # language1 ::= Hello World
print "language1:", language1
language2 = Group([Hello, World], max=-1) # language2 ::= [Hello World]+
print "language2:", language2
language3 = Group([Hello, World], min=0, max=-1) # language3 ::= [Hello World]*
print "language3:", language3
language4 = Group([Hello | World], min=0, max=-1) # language4 ::= [Hello | World]*
print "language4:", language4
language5 = Group([Hello, Group([','], min=0), World]) # language5 ::= [Hello [',']? World]
print "language5:", language5
# syntaxe 2
class Language6(Group): # == language 1
__group__ = [Hello, World]
print "language6:", Language6()
# utilisation de littéraux
class Language7(Group): # Language7 ::= [[[Hello ','] | [Hello Hello]] World]*
__group__ = Group([
[Group([Hello, ',']) | Group([Hello], min=2, max=2)], World
], min=0, max=-1)
print "language7:", Language7()
language8 = Group([Hello | World | "pif"])
print "language8:", language8