Translating Python's formal language into Rail Diagrams -


i trying translate python's formal grammar (https://docs.python.org/3/reference/grammar.html) rail diagrams. website using http://www.bottlecaps.de/rr/ui helpful of , have changed many things hand fir proper notation create rail diagram there still 50+ lines incorrect , hard fix brand new this. there easier way changing hand?

note website uses ebnf

thanks time,

write parser parses grammar, transform parse-tree required notation.

the transformation simple:

  • replace '#' comment introducers '//'
  • replace ':' tokens '::='
  • replace '[' tokens '('
  • replace ']' tokens ')?'

a suitable meta-grammar, in w3c notation, is

grammar  ::= rule+ eof rule     ::= nonterminal ':' alternatives alternatives          ::= alternative ( '|' alternative )* alternative          ::= ( symbol ( '*' | '+' )? )* symbol   ::= nonterminal            | terminal            | '(' alternatives ')'            | '[' alternatives ']'  <?tokens?>  nonterminal          ::= [a-z] [a-z_]* terminal ::= [a-z] [a-z_]*            | "'" [^']+ "'" eof      ::= $ ignorablewhitespace          ::= [ #x9#xa#xd]+            | '#' [^#xa]* [#xa]           /* ws: definition */ 

put in grammar.ebnf, use rex create parser it, coded e.g. in xquery, using command line:

   -xquery -tree 

this gives xquery module grammar.xquery. next, put python grammar in python.grammar, , xquery program in transform.xquery:

import module namespace p="grammar" @ "grammar.xquery"; declare option saxon:output "method=text"; declare variable $input xs:string external; $token in p:parse-grammar(unparsed-text($input))//text() return   if (starts-with(normalize-space($token), "#"))     replace($token, "((^|&#xa;)[\s])*#", "$1//")   else     switch ($token)     case ":" return "::="     case "[" return "("     case "]" return ")?"     default return $token 

then use saxon run it:

  java net.sf.saxon.query transform.xquery input=python.grammar > python.ebnf 

the result looking for.

of course can use favorite text editor global replaces achieving same. it's more fun right.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -