Skip to content

Commit

Permalink
done with EBNF ISO; 3 grammars recovered
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@1061 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed May 26, 2011
1 parent 92aca6e commit e79b185
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 959 deletions.
66 changes: 34 additions & 32 deletions topics/grammars/hunter.py
Expand Up @@ -2,8 +2,8 @@
# -*- coding: utf-8 -*-
import os, sys
import xml.etree.ElementTree as ET
#sys.path.append(os.getcwd().split('slps')[0]+'slps/shared/python')
import BGF
sys.path.append(os.getcwd().split('slps')[0]+'slps/shared/python')
import BGF3
from functools import reduce

debug = False
Expand Down Expand Up @@ -95,6 +95,8 @@ def reconsiderSpaces(ts,sep,vs):
vs = list(vs)
vs.append('\n')
for x in ts[1:]:
if x == '\n' and 'ignore-extra-newlines' in config.keys():
continue
if x == sep:
nts.append('')
elif nts[-1] in vs or x in vs:
Expand Down Expand Up @@ -403,7 +405,7 @@ def map2expr(ss):
j = len(ss)
if debug:
print('>>>context>>>',ss[i:j])
e = BGF.Star()
e = BGF3.Star()
e.setExpr(map2expr(ss[i+1:j-1]))
es.append(e)
i = j
Expand All @@ -414,7 +416,7 @@ def map2expr(ss):
j = len(ss)
if debug:
print('>>>context>>>',ss[i:j])
e = BGF.Plus()
e = BGF3.Plus()
e.setExpr(map2expr(ss[i+1:j-1]))
es.append(e)
i = j
Expand All @@ -425,7 +427,7 @@ def map2expr(ss):
j = len(ss)
if debug:
print('>>>context>>>',ss[i:j])
e = BGF.Optional()
e = BGF3.Optional()
e.setExpr(map2expr(ss[i+1:j-1]))
es.append(e)
i = j
Expand All @@ -439,17 +441,17 @@ def map2expr(ss):
if debug:
print('>>>context>>>',ss[i:j])
# {x y}* => (x (yx)*)?
e = BGF.Sequence()
e = BGF3.Sequence()
x = map2expr([ss[i+1]])
y = map2expr([ss[i+2]])
e.add(x)
e2 = BGF.Sequence()
e2 = BGF3.Sequence()
e2.add(y)
e2.add(x)
s = BGF.Star()
s.setExpr(BGF.Expression(e2))
e.add(BGF.Expression(s))
e2 = BGF.Optional()
s = BGF3.Star()
s.setExpr(BGF3.Expression(e2))
e.add(BGF3.Expression(s))
e2 = BGF3.Optional()
e2.setExpr(e)
es.append(e2)
i = j
Expand All @@ -463,16 +465,16 @@ def map2expr(ss):
if debug:
print('>>>context>>>',ss[i:j])
# {x y}+ => (x (yx)*)
e = BGF.Sequence()
e = BGF3.Sequence()
x = map2expr([ss[i+1]])
y = map2expr([ss[i+2]])
e.add(x)
e2 = BGF.Sequence()
e2 = BGF3.Sequence()
e2.add(y)
e2.add(x)
s = BGF.Star()
s.setExpr(BGF.Expression(e2))
e.add(BGF.Expression(s))
s = BGF3.Star()
s.setExpr(BGF3.Expression(e2))
e.add(BGF3.Expression(s))
es.append(e)
i = j
elif ss[i] == 'START-GROUP-SYMBOL':
Expand All @@ -496,7 +498,7 @@ def map2expr(ss):
elif ss[i][0] == config['start-terminal-symbol']:
if debug:
print('TERMINAL',ss[i][1:-1])
e = BGF.Terminal()
e = BGF3.Terminal()
if ss[i][1:-1] == '':
print('Serialisation error: empty terminal, replaced with ""!')
e.setName('""')
Expand All @@ -507,7 +509,7 @@ def map2expr(ss):
else:
if debug:
print('NONTERMINAL',ss[i])
e = BGF.Nonterminal()
e = BGF3.Nonterminal()
e.setName(ss[i])
es.append(e)
i += 1
Expand All @@ -518,28 +520,28 @@ def map2expr(ss):
elif len(ess) == 1:
if len(ess[0]) == 0:
print('Serialisation error: empty internal output sequence!')
return BGF.Expression(BGF.Epsilon())
return BGF3.Expression(BGF3.Epsilon())
elif len(ess[0]) == 1:
return BGF.Expression(ess[0][0])
return BGF3.Expression(ess[0][0])
else:
e = BGF.Sequence()
e = BGF3.Sequence()
for x in ess[0]:
e.add(BGF.Expression(x))
return BGF.Expression(e)
e.add(BGF3.Expression(x))
return BGF3.Expression(e)
else:
e = BGF.Choice()
e = BGF3.Choice()
for es in ess:
if len(es) == 0:
print('Serialisation error: empty internal output sequence!')
return BGF.Expression(BGF.Epsilon())
return BGF3.Expression(BGF3.Epsilon())
elif len(es) == 1:
e.add(BGF.Expression(es[0]))
e.add(BGF3.Expression(es[0]))
else:
ee = BGF.Sequence()
ee = BGF3.Sequence()
for x in es:
ee.add(BGF.Expression(x))
e.add(BGF.Expression(ee))
return BGF.Expression(e)
ee.add(BGF3.Expression(x))
e.add(BGF3.Expression(ee))
return BGF3.Expression(e)
print('Dead code reached!')
return

Expand Down Expand Up @@ -1001,9 +1003,9 @@ def useTerminatorToFixProds(ps,ts):
for p in prods:
print('\t',p[1],'is defined as:',p[2:])
# FINAL STEP: compose BGF
bgf = BGF.Grammar()
bgf = BGF3.Grammar()
for q in prods:
p = BGF.Production()
p = BGF3.Production()
if 'disregard-labels' not in config.keys() and q[0]:
p.setLabel(q[0])
p.setNT(q[1])
Expand Down
12 changes: 7 additions & 5 deletions topics/grammars/metasyntax/ebnf-iso-1/Makefile
@@ -1,7 +1,9 @@
include ../../Makefile.include

extract:
${tooldir}/xbgf generalize.xbgf ebnf.iso.informal.bgf ebnf-iso-1.bgf
cp src.8.1.txt src.prepared.txt
perl -pi -w -e 's/˜/~/g;' src.prepared.txt
perl -pi -w -e 's/ˆ/^/g;' src.prepared.txt
../../hunter.py src.prepared.txt config.edd ebnf-iso-1.raw.bgf
${tooldir}/xbgf post-extract.xbgf ebnf-iso-1.raw.bgf ebnf-iso-1.ext.bgf
${tooldir}/xbgf refactor.xbgf ebnf-iso-1.ext.bgf ebnf-iso-1.bgf

clean:
rm -f *.bnf *.html intermediate.lll tmp.xml *prepared*
include ../../Makefile.include
21 changes: 21 additions & 0 deletions topics/grammars/metasyntax/ebnf-iso-1/config.edd
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<edd:config xmlns:edd="http://planet-sl.org/edd">
<defining-symbol>=</defining-symbol>
<definition-separator-symbol>|</definition-separator-symbol>
<concatenate-symbol>,</concatenate-symbol>
<nonterminals-may-contain-spaces/>
<terminator-symbol>;</terminator-symbol>
<start-comment-symbol>(*</start-comment-symbol>
<end-comment-symbol>*)</end-comment-symbol>
<start-terminal-symbol>’</start-terminal-symbol>
<end-terminal-symbol>’</end-terminal-symbol>
<start-option-symbol>[</start-option-symbol>
<end-option-symbol>]</end-option-symbol>
<start-star-symbol>{</start-star-symbol>
<end-star-symbol>}</end-star-symbol>
<ignore-extra-newlines/>
<mask>
<token>"’"</token>
<terminal>’</terminal>
</mask>
</edd:config>

0 comments on commit e79b185

Please sign in to comment.