HTML isn't really context-free, for two reasons: First, SGML isn't and you want to support unknown tags (you need context-dependence to have an infinite number of closing tags to match the opening ones), secondly, and this one I'm not 100% certain about, there's rules (this tag can only show up in this context etc) that the spec states that can't be expressed in a CFG. In usual implementations, though, that's kind of checking is not done by the parser-as-such.
You can solve the infinite tags thing with a lexer hack, though. Practically all real-world computer languages that aren't context free can be parsed by collecting some information by a linear scan through everything, then passing the original text plus that information to a usual CFG parser (or, usually, even just LL/LR)
(Note: C is actually perfectly context-free... it just happens to be ambiguous without context information. But you can put it through a CFG parser, get back a parse forest expressing that ambiguity, and then resolve later).
Last, but not least: People are talking about "perl regexen". Those aren't regexen! They're more powerful than regular languages, and, therefore, among other things, dog-slow.
View all comments
Show parent comment
-1
u/barsoap Sep 08 '17
HTML isn't really context-free, for two reasons: First, SGML isn't and you want to support unknown tags (you need context-dependence to have an infinite number of closing tags to match the opening ones), secondly, and this one I'm not 100% certain about, there's rules (this tag can only show up in this context etc) that the spec states that can't be expressed in a CFG. In usual implementations, though, that's kind of checking is not done by the parser-as-such.
You can solve the infinite tags thing with a lexer hack, though. Practically all real-world computer languages that aren't context free can be parsed by collecting some information by a linear scan through everything, then passing the original text plus that information to a usual CFG parser (or, usually, even just LL/LR)
(Note: C is actually perfectly context-free... it just happens to be ambiguous without context information. But you can put it through a CFG parser, get back a parse forest expressing that ambiguity, and then resolve later).
Last, but not least: People are talking about "perl regexen". Those aren't regexen! They're more powerful than regular languages, and, therefore, among other things, dog-slow.