Skip to content

Instantly share code, notes, and snippets.

@trnila
Created April 17, 2017 21:53
Show Gist options
  • Save trnila/73f54ca6033f78c61bb77d5f820c371b to your computer and use it in GitHub Desktop.
Save trnila/73f54ca6033f78c61bb77d5f820c371b to your computer and use it in GitHub Desktop.
Pyparsing c prototype to golang
from pyparsing import *
class Function:
def __init__(self, tokens):
self.type = tokens[0]
self.name = tokens[1]
self.args = tokens[2] if len(tokens) > 2 else ""
def __repr__(self):
return "func {name}({args}) {type}".format(
name=self.name,
args=self.args,
type=self.type
)
class Arg:
def __init__(self, tokens):
self.type = tokens[0]
self.name = tokens[1]
def __repr__(self):
return "{} {}".format(self.name, self.type)
TYPES = Word("int") | Word("float") | Word("string")
ID = Word(alphas, alphanums)
ARG = TYPES + ID
ARG.setParseAction(Arg)
LANG = TYPES + ID + Suppress('(') + Optional(delimitedList(ARG)) + Suppress(');')
LANG.setParseAction(Function)
l = [
"int main();",
"int main(int a);",
"int main(int a, int b);",
"int main(int xax, float var17);",
"int main18(int xax, float var17);"
]
for i in l:
res = LANG.parseString(i)[0]
print("{} -> {}".format(i, res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment