Changeset 20742
- Timestamp:
- 10/05/08 02:05:11 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/python/dfareg/branches/codezine200809/dfareg/parser.py
r20730 r20742 6 6 class Parser(object): 7 7 """ 8 NFAを組み立てる8 構文解析器 9 9 expression -> subexpr 10 10 subexpr -> seq '|' subexpr | seq … … 17 17 self.lexer = lexer 18 18 self.look = None 19 # 最初の文字を読む 19 20 self.move() 20 21 21 22 def match(self, tag): 22 if self.look.kind != tag: raise "syntax error" 23 if self.look.kind != tag: 24 # 予期せぬトークンが来たら、エラー終了 25 raise "syntax error" 23 26 self.move() 24 27 … … 27 30 28 31 def expression(self): 32 # expression -> subexpr 29 33 node = self.subexpr() 34 35 # トークンがすべて処理されたか 36 self.match(Talken.EOF) 37 38 # 構文木を実行し、NFAを作る 30 39 context = Context() 31 40 fragment = node.assemble(context) … … 33 42 34 43 def subexpr(self): 44 # subexpr -> seq '|' subexpr | seq 35 45 node = self.seq() 36 46 if self.look.kind == Talken.OPE_UNION: … … 43 53 if self.look.kind == Talken.LPAREN \ 44 54 or self.look.kind == Talken.CHARACTER: 55 # seq -> star seq 45 56 node1 = self.star() 46 57 node2 = self.seq() 47 58 node = Concat(node1, node2) 48 59 return node 49 # NOP (ε)60 # seq -> '' 50 61 return Character("") 51 62 52 63 def star(self): 64 # star -> factor '*' | factor 53 65 node = self.factor() 54 66 if self.look.kind == Talken.OPE_STAR: … … 59 71 def factor(self): 60 72 if self.look.kind == Talken.LPAREN: 73 # factor -> '(' subexpr ')' 61 74 self.match(Talken.LPAREN) 62 75 node = self.subexpr() … … 64 77 return node 65 78 elif self.look.kind == Talken.CHARACTER: 79 # factor -> CHARACTER 66 80 node = Character(self.look.value) 67 81 self.match(Talken.CHARACTER);
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)