Antlr4: Go target emits invalid Go for PHP lexer

Created on 25 Sep 2017  Â·  9Comments  Â·  Source: antlr/antlr4

$ git clone https://github.com/antlr/grammars-v4.git
$ cd grammars-v4/php
$ antlr4 -Dlanguage=Go PHPLexer.g4
$ go build
# _/Users/willfaught/grammars-v4/php
./php_lexer.go:1414:1: syntax error: non-declaration statement outside function body
./php_lexer.go:1423:1: invalid character U+0040 '@'
./php_lexer.go:1476:84: unknown escape sequence
./php_lexer.go:1509:40: invalid character U+003F '?'
./php_lexer.go:1510:30: invalid character U+003F '?'
./php_lexer.go:1568:19: syntax error: unexpected newline, expecting { after if clause
./php_lexer.go:1570:24: syntax error: unexpected newline, expecting { after if clause
./php_lexer.go:1574:8: syntax error: unexpected else, expecting }
./php_lexer.go:1575:8: syntax error: unexpected { at end of statement
./php_lexer.go:1580:4: syntax error: non-declaration statement outside function body
./php_lexer.go:1580:4: too many errors

Line ./php_lexer.go:1414:

1401 // PHPLexer modes.
1402 const (
1403   PHPLexerINSIDE = iota + 1
1404   PHPLexerHtmlQuoteStringMode
1405   PHPLexerHtmlDoubleQuoteStringMode
1406   PHPLexerSCRIPT
1407   PHPLexerSTYLE
1408   PHPLexerPHP
1409   PHPLexerInterpolationString
1410   PHPLexerSingleLineCommentMode
1411   PHPLexerHereDoc
1412 )
1413 
1414 public boolean AspTags = true;
1415 boolean _scriptTag;
1416 boolean _styleTag;
1417 String _heredocIdentifier;
1418 int _prevTokenType;
1419 String _htmlNameText;
1420 boolean _phpScript;
1421 boolean _insideString;
1422 
1423 @Override
1424 public Token nextToken()

The problem is that lines 1414–1421 are Java syntax, not Go.

I'm not sure how the tests are even passing if some of the Go target sub-templates haven't been updated to emit Go instead of Java. Is this particular target sub-template rarely invoked? @parrt @pboyer

  • [x] I am not submitting a question on how to use ANTLR; instead, go to antlr4-discussion google group or ask at stackoverflow
  • [x] I have done a search of the existing issues to make sure I'm not sending in a duplicate
code-gen tool invalid go bug

All 9 comments

This grammar is language-dependent. Only Java, C#, and Python targets available. This issue is not related to the current implementation of ANTLR, please close.

How is it language-dependent? The Go target should be able to handle anything.

Sent from my iPhone

On Sep 25, 2017, at 11:04 AM, Ivan Kochurkin notifications@github.com wrote:

This grammar is language-dependent. Only Java, C#, and Python targets available.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

PHP grammar uses language-dependent (Java, C#, Python) actions and predicates. You can translate PHP grammar to Go runtime.

That definitely points to a problem with the go target. You should be able to generate code that compiles using the Go target for any grammar. unfortunately I don't know go so I can't help.

In this way, other targets (C++, JavaScript, Swift) also have problems because they can't compile Java code?

oh! there are java actions? that's why. it's not the generated code. public boolean AspTags = true; ha! sorry. closing.

@parrt I don't follow why this was closed. Isn't this a bug with one or more Go target templates (in that it was never changed from Java syntax to Go syntax)?

Oh, it's somebody with JAVA code public boolean AspTags = true; in the grammar. no way to auto-translate to Go or whatever. it's a grammar issue. look inside the grammar :) you'll see actions.

Oh, I see! Makes sense.

Sent from my iPhone

On Sep 25, 2017, at 3:47 PM, Terence Parr notifications@github.com wrote:

Oh, it's somebody with JAVA code public boolean AspTags = true; in the grammar. no way to auto-translate to Go or whatever. it's a grammar issue. look inside the grammar :) you'll see actions.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Was this page helpful?
0 / 5 - 0 ratings