antlr / antlr/antlr3

ANTLR C target follow set generates reference to undeclared identifier

Open
#99 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

target:c
Dominant language
Java
Stars
253
Forks
178
PR merge metrics
No merged PRs in 30d

Description

Copied from [Stack Overflow](http://stackoverflow.com/questions/14892689):

ANTLRWorks 1.5rc1 successfully generated the C target code for this grammar with no warnings or errors but the C target code would not compile.

ANTLRWorks 1.5 generated the same bad C target code but the console listed many template errors.

ANTLRWorks 1.4.3 generated valid C target code that compiles without error.

```
error C2065: 'FOLLOW_set_in_sqlCOMP_OP2185' : undeclared identifier
```

Rule `sqlCOMP_OP` is referenced from multiple boolean expression rules

All of the rules that generated references to undefined identifiers were of the form:

```
fule: (Tokena | Tokenb | Tokenc)? Tokend;
```

or there were multiple references to a common rule that was of the form:

```
fule: (Tokena | Tokenb | Tokenc);
```

In the first case I was able to transform the rule to a logically equivalent form that did not generate a reference to an undefined identifier:

```
fule: (Tokena Tokend | Tokenb Tokend | Tokenc Tokend | Tokend);
```

In the second case, no transformation is possible. Instead the only fix is to back-substitute the body of the failing rule into every reference.

```
sqlCONDITION :
sqlLOGICAL_EXPRESSION
;

sqlLOGICAL_EXPRESSION :
sqlLOGICAL_TERM (STOK_OP_OR sqlLOGICAL_TERM)*
;

sqlLOGICAL_TERM :
sqlLOGICAL_FACTOR (STOK_OP_AND sqlLOGICAL_FACTOR)*
;

sqlLOGICAL_FACTOR :
(STOK_NOT) => STOK_NOT
( sqlUNTYPED_BOOLEAN_PRIMARY
| sqlNUMERIC_BOOLEAN_PRIMARY
| sqlSTRING_BOOLEAN_PRIMARY
| sqlCLOB_BOOLEAN_PRIMARY
| sqlDATE_BOOLEAN_PRIMARY
| sqlDATETIME_BOOLEAN_PRIMARY
| STOK_OPEN_PAREN
sqlCONDITION
STOK_CLOSE_PAREN
)
;

sqlUNTYPED_BOOLEAN_PRIMARY :
( STOK_EXISTS sqlSUB_QUERY
| STOK_NOT STOK_EXISTS sqlSUB_QUERY
| sqlIS_OR_ISNOT_CLAUSE
STOK_IN STOK_ROWSET STOK_IDENTIFIER
)
;

sqlCOMP_OP :
( STOK_OP_EQ
| STOK_OP_NE
| STOK_OP_GE
| STOK_OP_GT
| STOK_OP_LE
| STOK_OP_LT
)
;

sqlIS_OR_ISNOT_CLAUSE :
( STOK_IS STOK_NOT?
| STOK_NOT
)
;

sqlNUMERIC_BOOLEAN_PRIMARY :
( sqlNUMERIC_EXPRESSION
( sqlCOMP_OP
sqlNUMERIC_EXPRESSION

| sqlNUMERIC_BOOLEAN_PREDICATE
)

| sqlNUMERIC_COLUMN_LIST
sqlNUMERIC_BOOLEAN_PREDICATE
)
;

sqlNUMERIC_BOOLEAN_PREDICATE:
( sqlIS_OR_ISNOT_CLAUSE?
( STOK_IN sqlNUMERIC_SET
| STOK_BETWEEN sqlNUMERIC_EXPRESSION STOK_OP_AND sqlNUMERIC_EXPRESSION
)
| sqlIS_OR_ISNOT_CLAUSE
STOK_SQL_NULL
)
;

sqlSTRING_BOOLEAN_PRIMARY :
( sqlSTRING_EXPRESSION
( sqlCOMP_OP
sqlSTRING_EXPRESSION

| sqlSTRING_BOOLEAN_PREDICATE
)

| sqlSTRING_COLUMN_LIST
sqlSTRING_BOOLEAN_PREDICATE
)
;

sqlSTRING_BOOLEAN_PREDICATE :
( sqlIS_OR_ISNOT_CLAUSE?
( STOK_IN sqlSTRING_SET
| STOK_LIKE sqlSTRING
| STOK_BETWEEN sqlSTRING_EXPRESSION STOK_OP_AND sqlSTRING_EXPRESSION
)
| sqlIS_OR_ISNOT_CLAUSE
STOK_SQL_NULL
)
;

sqlCLOB_BOOLEAN_PRIMARY :
( STOK_NOT?
STOK_CONTAINS
STOK_OPEN_PAREN
sqlCLOB_COLUMN_VALUE
STOK_COMMA
sqlSTRING
STOK_CLOSE_PAREN

| sqlCLOB_COLUMN_VALUE
sqlIS_OR_ISNOT_CLAUSE
STOK_SQL_NULL
)
;

sqlDATE_BOOLEAN_PRIMARY :
( sqlDATE_EXPRESSION
( sqlCOMP_OP
sqlDATE_EXPRESSION

| sqlDATE_BOOLEAN_PREDICATE
)

| sqlDATE_COLUMN_LIST
sqlDATE_BOOLEAN_PREDICATE
)
;

sqlDATE_BOOLEAN_PREDICATE :
( sqlIS_OR_ISNOT_CLAUSE?
( STOK_IN sqlDATE_SET
| STOK_BETWEEN sqlDATE_EXPRESSION STOK_OP_AND sqlDATE_EXPRESSION
)
| sqlIS_OR_ISNOT_CLAUSE
STOK_SQL_NULL
)
;

sqlDATETIME_BOOLEAN_PRIMARY :
( sqlDATETIME_EXPRESSION
( sqlCOMP_OP
sqlDATETIME_EXPRESSION

| sqlDATETIME_BOOLEAN_PREDICATE
)

| sqlDATETIME_COLUMN_LIST
sqlDATETIME_BOOLEAN_PREDICATE
)
;

sqlDATETIME_BOOLEAN_PREDICATE :
( sqlIS_OR_ISNOT_CLAUSE?
( STOK_IN sqlDATETIME_SET
| STOK_BETWEEN sqlDATETIME_EXPRESSION STOK_OP_AND sqlDATETIME_EXPRESSION
)
| sqlIS_OR_ISNOT_CLAUSE
STOK_SQL_NULL
)
;
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported grammar with the ANTLR C target and inspect the generated references to FOLLOW_set_in_sqlCOMP_OP2185. Compare output from ANTLRWorks 1.5rc1 with 1.4.3; done means the generated C target declares every referenced follow-set identifier and compiles without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.