3 ms·
Does anyone have any examples of popular languages that are very easy to parse or ones that have a lot of ambiguity? I assume the lispy type languages fall int
by tyingq 10y ago
Does anyone have any examples of popular languages that are very easy to parse or ones that have a lot of ambiguity?
I assume the lispy type languages fall into the easily parsed bucket, perhaps tcl as well? And Perl may be a good example of a language that has notable ambiguity?
- bandrami 10y agoForth is probably the simplest to parse, and when you write a forth you usually write your parser in assembly. It consists of "start at the first non-whitespace character of the pad and continue until you reach another whitespace character; that's a token" (though "token" has a different meaning in forth; it's a "word"). Lisp seems like it should be nearly as simple: recursively read in a balanced s-expression and attempt to intern the atoms in that s-expression; error out if any of those steps fails. However, Common Lisp's scanner/parser (called the "reader") allows the user to define arbitrary macros; basically the scanner has access to the full compiler if you want it to, so it can get really complicated really quickly.
- bandrami 10y agoAnd, just to finish that thought, "read macros" aren't some abstract corner case of interest to theoreticians; they are how a whole lot of the language and libraries (e.g. complex numbers, the regex library, etc.) are implemented.
- Drup 10y agoRaw lisp is obviously very easy, not sure about the various schemes. OCaml, the whole SML family are easy to parse (just an LR grammar that is easy to copy). I think Rust is the same. IIRC, Java is LL(k), so very easy. C is not LR(1) for annoying reasons (typedef). Same for Go, IIRC. Otherwise, it's okayish. Javascript has this inane "optional semicolon" thing, which probably makes it annoying. C++ is probably the most insane thing to parse. And then, there is bash[1]. http://www.oilshell.org/blog/2016/10/20.html http://www.oilshell.org/blog/2016/10/20.html
- steveklabnik 10y agoRust is 99% easy to parse, but there's one, rarely used, syntactic construct that's context-sensitive.
- saosebastiao 10y agoWhich one is that?
- steveklabnik 10y agoRaw string literals https://doc.rust-lang.org/stable/reference.html#raw-string-literals https://doc.rust-lang.org/stable/reference.html#raw-string-l... and raw byte string literals https://doc.rust-lang.org/stable/reference.html#raw-byte-string-literals https://doc.rust-lang.org/stable/reference.html#raw-byte-str... r"…", r#"…"#, r##"…"## br"…", br#"…"#, br##"…"## you need to count the #s.
- munificent 10y agoSmalltalk is famously easy to parse. They always proudly said you can fit the entire grammar on a single page. It's a really compact, neat syntax. Pascal is also pretty clean and regular. Unlike Smalltalk, it was designed more to make the compiler's job easier, so not only is the grammar pretty regular, but it's organized such that you can parse and compile to native code in a single pass. C is a pain to parse, it's context-sensitive in some areas. C++ inherits all of that and adds a lot more on top. My understanding is that Perl can't be parsed without being able to execute Perl code since there are some features that let you extend the grammar in Perl itself?
- rurban 10y agoPerl is mostly hard to parse because it using a dynamic lexer. The parser itself uses a normal static yacc grammar, but the lexer drives the parse dynamically. Eg if a class or method is already defined or not, and what types to expect as arguments. It's crazy, but beautiful. Not so insane, more like a very clever hack.
- zeven7 10y agoJavaScript's not exactly "easy" to parse (though "easy" is subjective), but it is (mostly?) parseable with an LR(1) parser (only requires lookahead of 1 character) -- though it does require reinterpretation.
- chubot 10y ago* Easy: Python, Java. Python generates a top-down parser from Grammar/Grammar using its own pgen.c system, and the Java spec has a grammr. This doesn't mean there's no ambiguity, but they are easier. * Pretty easy: JavaScript, Go. I think the semi-colon insertion rule kind of breaks the lexer/grammar model, but they are still easy to parse. * Hard: - C (http://eli.thegreenplace.net/2007/11/24/the-context-sensitivity-of-cs-grammar) - Ruby (related thread: https://news.ycombinator.com/item?id=13825975) - OCaml -- AFAIK, a naive yacc-style grammar will generate tons of shift-reduce conflicts - Haskell -- based on hearsay, I think this is hard * Insane: C++, Perl, to some extent bash. See the end of my post "Parsing Bash is Undecidable" for links: http://www.oilshell.org/blog/2016/10/20.html http://www.oilshell.org/blog/2016/10/20.html I'm not as familiar with languages like PHP and C#, but they're somewhere between the easy and hard categories.