Ignore COBOL comments within string literals
Fixes COBOL lexer to not treat '*>' as comments inside string constants.
This commit updates the COBOL compiler’s lexer to correctly handle comments within string literals. Previously, the lexer would incorrectly interpret the sequence ’*>’ as an inline comment even if it appeared inside a quoted string. The fix introduces a new function, skip_quoted_literal, to properly recognize string constants and ensures that inline comments are only processed when they appear outside of these strings. This prevents erroneous parsing of COBOL source code.
In Details
The gcc/cobol/lexio.cc file handles the lexical analysis of COBOL source code. This change modifies the lexer's behavior regarding inline comments, which are typically denoted by '*>' in COBOL. The issue was that the lexer did not correctly distinguish between comment markers and literal string content, leading to incorrect parsing when '*>' appeared within a string literal. A new function, skip_quoted_literal, is introduced to manage the parsing of string constants, ensuring that comment detection is bypassed within them. This corrects a defect in the lexer's state management for string…
- lexer
- A lexical analyzer, also known as a scanner or tokenizer. It is the first phase of a compiler, responsible for breaking the input source code into a stream of tokens (meaningful lexemes).
- string literal
- A sequence of characters enclosed in quotation marks that represents a string value in a programming language. The content within the quotes is treated as data, not as executable code or commands.
- inline comment
- A comment embedded within a line of code. In COBOL, the sequence '*>' often signifies the start of an inline comment.
- lexical analysis
- The process of converting a sequence of characters (source code) into a sequence of meaningful lexical units called tokens. This is the first stage in compilation.