Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to write your own <a href="http://en.wikipedia.org/wiki/Recursive_descent_parser" rel="nofollow noreferrer">Recursive Descent Parser</a> from your BNF/EBNF. I had to write my own recently and <a href="http://www.cis.upenn.edu/~matuszek/General/recursive-descent-parsing.html" rel="nofollow noreferrer">this page</a> was a lot of help. I'm not sure what you mean by "with just code". Do you mean you want to know how to write your own recursive parser?</p> <p>If you want to do that, you need to have your grammar in place first. Once you have your EBNF/BNF in place, the parser can be written quite easily from it.</p> <p>The first thing I did when I wrote my parser, was to read everything in and then tokenize the text. So I essentially ended up with an array of tokens that I treated as a stack. To reduce the verbosity/overhead of pulling a value off a stack and then pushing it back on if you don't require it, you can have a <code>peek</code> method that simply returns the top value on the stack without popping it.</p> <p><strong>UPDATE</strong></p> <p>Based on your comment, I had to write a recursive-descent parser in Javascript from scratch. You can take a look at the parser <a href="http://github.com/vivin/regula/blob/master/regula.js" rel="nofollow noreferrer">here</a>. Just search for the <code>constraints</code> function. I wrote my own <code>tokenize</code> function to tokenize the input as well. I also wrote another convenience function (<code>peek</code>, that I mentioned before). The parser parses according to the EBNF <a href="http://github.com/vivin/regula/blob/master/constraint-definition.ebnf" rel="nofollow noreferrer">here</a>.</p> <p>This took me a little while to figure out because it's been years since I wrote a parser (last time I wrote it was in school!), but trust me, once you get it, you <em>get</em> it. I hope my example gets your further along on your way. </p> <p><strong>ANOTHER UPDATE</strong></p> <p>I also realized that my example may not be what you want because you might be going towards using a shift-reduce parser. You mentioned that right now you are trying to write a tokenizer. In my case, I did write my own tokenizer in Javascript. It's probably not robust, but it was sufficient for my needs.</p> <pre><code> function tokenize(options) { var str = options.str; var delimiters = options.delimiters.split(""); var returnDelimiters = options.returnDelimiters || false; var returnEmptyTokens = options.returnEmptyTokens || false; var tokens = new Array(); var lastTokenIndex = 0; for(var i = 0; i &lt; str.length; i++) { if(exists(delimiters, str[i])) { var token = str.substring(lastTokenIndex, i); if(token.length == 0) { if(returnEmptyTokens) { tokens.push(token); } } else { tokens.push(token); } if(returnDelimiters) { tokens.push(str[i]); } lastTokenIndex = i + 1; } } if(lastTokenIndex &lt; str.length) { var token = str.substring(lastTokenIndex, str.length); token = token.replace(/^\s+/, "").replace(/\s+$/, ""); if(token.length == 0) { if(returnEmptyTokens) { tokens.push(token); } } else { tokens.push(token); } } return tokens; } </code></pre> <p>Based on your code, it looks like you are reading, tokenizing, and parsing at the same time - I'm assuming that's what a shift-reduce parser does? The flow for what I have is tokenize first to build the stack of tokens, and then send the tokens through the recursive-descent parser.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload