Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good CSS grammar in Antlr v4

Are there any good CSS grammars out there for antlr4? I know there are some grammars for antlr3, but it turns out CSS is not trivial to parse without "lexer modes", which were added in v4. Why?

Consider the following CSS selectors:

.hello.world { /* ... */ }
.hello .world { /* ... */ }

In most grammars, whitespace is simply ignored. But if you ignore whitespace, it becomes impossible to distinguish between the two selectors above at the parser level.

Then again, if you don't ignore whitespace, the grammar becomes pretty noisy with WS? or WS* patterns everywhere, since whitespace is mostly meaningless unless it occurs within a selector.

Which is where modes from antlr4 come in, because with support for lexer modes you can define new rules for the lexer whenever you enter different contexts (i.e. don't ignore whitespace within the "selector" context).

That said, I'll accept any grammar for antlr3 as well so long as it handles whitespaces properly, as that's the version we're using now anyway ;-)

like image 807
gzak Avatar asked Sep 03 '13 23:09

gzak


People also ask

What grammar does ANTLR use?

ANTLR 2 accepts three types of grammar specifications -- parsers, lexers, and tree-parsers (also called tree-walkers). Because ANTLR 2 uses LL(k) analysis for all three grammar variants, the grammar specifications are similar, and the generated lexers and parsers behave similarly.

Is ANTLR LL or LR?

In computer-based language recognition, ANTLR (pronounced antler), or ANother Tool for Language Recognition, is a parser generator that uses LL(*) for parsing. ANTLR is the successor to the Purdue Compiler Construction Tool Set (PCCTS), first developed in 1989, and is under active development.

What is ANTLR v4?

ANTLR v4 is a powerful tool used for building new programming languages and processing/translating structured text or binary files. ANTLR uses a grammar you create to generate a parser which can build and traverse a parse tree (or abstract syntax tree, AST).


1 Answers

I imagine you've already found an answer to this question as it was posted so long ago. Even still:

A good Antlr v4 grammar for CSS3 can be found on the Antlr Github here:

Antlr v4 CSS3 Grammar

As stated in the Readme, it works on a number of CSS files - however, it doesn't handle the full syntax of @import or @include.

like image 81
Daniel Arthur Avatar answered Oct 13 '22 17:10

Daniel Arthur