Hey everyone it's spud again! reciently I've been looking into writing a simple parser to put into my userscript. The only problem is that when ever I try to write one I come up with problems. So now I'm turning to one of you guys to write it. I know that its generally frowned upon to have someone else write your code for you but all I want here is a base to work off of. I cant seem to find a way to parse code from the inside out. If I could use this method I would be able to make a simple parser to read little bits of code put into posts and other web media. Basically the way I want it to work is like this... First it finds the inner most command. Then executes outward, allowing to use the return values of each command in the command that contains it. If anyone can give me a simple bit of code to work off of that would be great! Thanks!
-spud2451
What exactly is it that you're parsing?

In any case, the answer, as usual, is "define a grammar, code it up in flex + bison (or equivalent), and compile".
well I'm trying to parse commands. Something like "echo(input('Hello whats your name?'))" I guess. The thing is that I don't know how to write a parser and I want to be able to make it in javascript.
Well I figured it out by taking one of my old parser drafts and trying to fix a few problems. Here's the code who wants to build off of it. Keep in mind that's its really simple though, so it can only parse single lines of code and not conditionals. Also it doesn't have quote escapes or concatenations built in. But for those of you interested here you go!

Python code:

Code:
pos = 0
progvars = {}

def parse(codestr):
    letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz _-"
    curchar = ""
    parens = 0
    quotes = 0
    results = []
    curword = ""
    global pos
    while curchar != ")":
        curchar = codestr[pos:pos+1]
        if curchar in letters:
            curword += curchar
        elif quotes and curchar != '"':
            curword += curchar
        if curchar == "(":
            if parens == 0:
                parens = 1
                results.append(curword)
                curword = ""
            else:
                pos -= len(curword)
                results.append(parse(codestr))
        if curchar == ",":
            results.append(curword)
            curword = ""
        if curchar == '"':
            if quotes == 0:
                quotes = 1
            else:
                quotes = 0
            curword += curchar
        pos += 1
    pos -= 1
    results.append(curword)
    return functions(results)

def functions(results):
    global progvars
    if results[0] == "echo":
        print(eval(results[1]))
        return results[1]
    if results[0] == "input":
        return '"'+raw_input(eval(results[1]))+'"'
    if results[0] == "set_var":
        progvars[eval(results[1])] = results[2]
        return results[2]
    if results[0] == "get_var":
        return progvars[eval(results[1])]
    return "0"


code = 'set_var("name",input("whats your name: "));echo("Hello");echo(get_var("name"))'


for i in code.split(";"):
    parse(i)
    pos = 0
Interesting, you've essentially created an extremely simple lexer. As Elfprince13 said, flex+bison is the way to go with any compiler project, unless you have a very good reason for creating your own lexer. In my undergrad Compilers class, one of my smartest colleagues decided to code a lexer from scratch, and hadn't even finished the lexer yet at the end of the semester when the rest of us were compiling and executing complete C programs with our compilers.
Well it just takes a little bit of thought. I actually used nested functions to do most of the work for me.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement