https://gcc.gnu.org/g:f41154c40ae30af61a81092de9c6de5d461a6623

commit r17-6598-gf41154c40ae30af61a81092de9c6de5d461a6623
Author: James K. Lowden <[email protected]>
Date:   Sun Oct 25 22:24:15 2026 -0400

    cobol: Recognize user-defined words that are almost numeric literals.
    
    Fix RT 3727.
    
    COBOL allows user-defined words to be all-numeric if they have one
    letter.  The name 2E0 is valid; 2.E0 is floating-point literal.
    
    gcc/cobol/ChangeLog:
    
            * scan.l: Return NAME token if new function reports non-numeric.
            * scan_ante.h (dot_like): Check for comma/dot.
            (lex_numeric_literal): New function.

Diff:
---
 gcc/cobol/scan.l      |  8 ++++++--
 gcc/cobol/scan_ante.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/gcc/cobol/scan.l b/gcc/cobol/scan.l
index 5e2e4d90cec9..1d44d9d963e0 100644
--- a/gcc/cobol/scan.l
+++ b/gcc/cobol/scan.l
@@ -1262,10 +1262,14 @@ USE({SPC}FOR)?          { return USE; }
                          //  the literal except as the rightmost character."
                          size_t len = strlen(s);
                          assert(len);
-                         if( s[--len] == '.' ) {
-                           s[len] = '\0';
+                         if( s[len - 1] == '.' ) {
+                           s[--len] = '\0';
                            myless(len);
                          }
+                          if( ! is_numeric_literal(len, s) ) {
+                            ydflval.string = yylval.string = s;
+                            return NAME;
+                          }
                          numstr_of(s); free(s);
                          return NUMSTR;
                        }
diff --git a/gcc/cobol/scan_ante.h b/gcc/cobol/scan_ante.h
index 01cb985b6d49..93a830d4533d 100644
--- a/gcc/cobol/scan_ante.h
+++ b/gcc/cobol/scan_ante.h
@@ -809,6 +809,56 @@ symbol_exists( const char name[] ) {
   return e;
 }
 
+/*
+ * 8.3.3.3.3 Floating-point numeric literals 2) The literal to the left of the
+ * 'E' represents the significand. It may be signed and shall include a decimal
+ * point.
+ */
+static bool
+is_numeric_literal( size_t len, const char input[] ) {
+  const char * enput = input + len;
+
+  if( *input == '+' || *input == '-' ) ++input;
+  gcc_assert(input < enput);
+  // lexer dfc pattern admits trailing dot/comma.
+  if( enput[-1] == '.' || enput[-1] == ',' ) enput--;
+
+  auto p = std::find(input, enput, symbol_decimal_point());
+
+  bool digits_only = std::all_of( input, p,
+                                  [](char ch) {
+                                    return ISDIGIT(ch);
+                                  } );
+  if( digits_only ) {
+    if( p == enput ) return true; // integer, no decimal point
+
+    ++p;
+    // fixed or floating point
+    auto pE = std::find_if(p, enput,
+                           [](char ch) {
+                             return 'E' == TOUPPER(ch);
+                           } );
+    if( pE < enput ) { // floating point
+      digits_only = std::all_of(p, pE,
+                                [](char ch) {
+                                  return ISDIGIT(ch);
+                                } );
+      if( ! digits_only ) {
+        return false; // non-numeric "fraction" between . and E
+      }
+      p = ++pE;
+      if( *p == '+' || *p == '-' ) ++p;
+      if( p == enput ) return false;    // no digits after E
+      if( p + 5 < enput ) return false; // up to 4 digits
+    }
+    return std::all_of(p, enput,
+                       [](char ch) {
+                         return ISDIGIT(ch);
+                           } ); // all digits after decimal point or E
+  }
+  return false;
+}
+
 static int
 typed_name( const char name[] ) {
   if( 0 == PROGRAM ) return NAME;

Reply via email to