Author Topic: Please help with this C syntax error  (Read 6879 times)

0 Members and 1 Guest are viewing this topic.

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6146
  • Country: gb
  • Doing electronics since the 1960s...
Please help with this C syntax error
« on: June 05, 2025, 09:31:17 pm »
Code: [Select]

#define GCODE    const
#define FCODE    /* nothing */
#define SGUCHAR uint8_t

typedef struct _GPALETTE_RGB
   {
   SGUCHAR r;
   SGUCHAR g;
   SGUCHAR b;
   } GPALETTE_RGB;


#ifdef LCD_CONTROLLER_ST7789
static GCODE GPALETTE_RGB FCODE ghw_palette[GHW_PALETTE_SIZE] =
#include <gcolor_4_ST7789.pal>
;
#endif

and the .pal file contains this

Code: [Select]
/*V P C 0 16 8 8 8 8 16 24 0  gcolor_4.pal   LCDIcon FileDescriptor: Do not edit or move */
{
{0x00U, 0x00U, 0x00U},
{0xFFU, 0x00U, 0x00U},
{0xC0U, 0x80U, 0x00U},
{0xFFU, 0x80U, 0x00U},
{0xFFU, 0xFFU, 0x00U},
{0x00U, 0xFFU, 0x00U},
{0x00U, 0xC0U, 0x00U},
{0x00U, 0xFFU, 0xFFU},
{0x00U, 0x80U, 0xFFU},
{0x00U, 0x00U, 0xFFU},
{0x80U, 0x00U, 0xFFU},
{0xA0U, 0xA0U, 0xFFU},
{0xFFU, 0x00U, 0xFFU},
{0x80U, 0x80U, 0x80U},
{0xC0U, 0xC0U, 0xC0U},
{0xFFU, 0xFFU, 0xFFU}
}

In Cube IDE I get an undefined "syntax error" in the editor, but it compiles without apparent errors. Can't tell if it runs because I am days away from getting to a buildable state (it is a commercial graphics library; a huge mess of very similar files for different LCD controllers).

I thought the "#define FCODE    /* nothing */" is a problem but removing the FCODE makes no difference.

The syntax error is showing on the three lines starting with static GCODE GPALETTE_...
« Last Edit: June 05, 2025, 09:46:34 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12507
  • Country: us
    • Personal site
Re: Please help with this C syntax error
« Reply #1 on: June 05, 2025, 10:03:58 pm »
Have you tried to remove the include? It is likely that Cube parser does not understand inclusion of a *.pal file.

It might also not like that "#include" does not start at the beginning of a line. Technically at least '#' need to be at the beginning and 'include' may be aligned any way. But i don't know if any checker is that strict.
Alex
 
The following users thanked this post: peter-h

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 3307
  • Country: us
Re: Please help with this C syntax error
« Reply #2 on: June 05, 2025, 10:06:53 pm »
Is there a reason for separating the declaration from the data?

Try this:

Code: [Select]
#define GCODE    const
#define FCODE    /* nothing */
#define SGUCHAR uint8_t

typedef struct _GPALETTE_RGB
{
   SGUCHAR r;
   SGUCHAR g;
   SGUCHAR b;
} GPALETTE_RGB;


#ifdef LCD_CONTROLLER_ST7789
   #include <gcolor_4_ST7789.pal>
#endif

Code: [Select]
/*V P C 0 16 8 8 8 8 16 24 0  gcolor_4.pal   LCDIcon FileDescriptor: Do not edit or move */
static GCODE GPALETTE_RGB FCODE ghw_palette[GHW_PALETTE_SIZE] =
{
   0x00U, 0x00U, 0x00U,
   0xFFU, 0x00U, 0x00U,
   0xC0U, 0x80U, 0x00U,
   0xFFU, 0x80U, 0x00U,
   0xFFU, 0xFFU, 0x00U,
   0x00U, 0xFFU, 0x00U,
   0x00U, 0xC0U, 0x00U,
   0x00U, 0xFFU, 0xFFU,
   0x00U, 0x80U, 0xFFU,
   0x00U, 0x00U, 0xFFU,
   0x80U, 0x00U, 0xFFU,
   0xA0U, 0xA0U, 0xFFU,
   0xFFU, 0x00U, 0xFFU,
   0x80U, 0x80U, 0x80U,
   0xC0U, 0xC0U, 0xC0U,
   0xFFU, 0xFFU, 0xFFU
};
 

Offline gamalot

  • Super Contributor
  • ***
  • Posts: 1985
  • Country: au
  • Correct my English
    • Youtube
Re: Please help with this C syntax error
« Reply #3 on: June 05, 2025, 10:08:08 pm »
Have you defined GHW_PALETTE_SIZE?
I'm a poet, I didn't even know it. |  https://youtube.com/@gamalot | https://github.com/gamalot
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6146
  • Country: gb
  • Doing electronics since the 1960s...
Re: Please help with this C syntax error
« Reply #4 on: June 06, 2025, 05:38:14 am »
Quote
Have you tried to remove the include? It is likely that Cube parser does not understand inclusion of a *.pal file.

Indeed; that is it. I also tried .h but no difference. The Cube parser simply does not like the

   static blah blah ghw_palette[GHW_PALETTE_SIZE] =
   #include <anyfile>

The reason for having the data via a #include is because there is a palette editor which operates on these files, so keeping them externally is desirable. In reality I am using 16 bit colour so should not need a palette anyway. With 8 bit colour one might need it, for a RGB LCD display. TBH I am learning about this stuff...

Thank you all. I thought I was doing something really dumb.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5745
  • Country: Earth
Re: Please help with this C syntax error
« Reply #5 on: June 06, 2025, 11:36:38 am »
try to remove these preprocessor blah blah substitutions and replace with normal declaration:
Code: [Select]
static const GPALETTE_RGB ghw_palette[GHW_PALETTE_SIZE] = {
};
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17841
  • Country: fr
Re: Please help with this C syntax error
« Reply #6 on: June 06, 2025, 03:48:10 pm »
Have you defined GHW_PALETTE_SIZE?

And, I think we have a winner here. That would be the only missing part that I can spot as well.
I don't see a definition for GHW_PALETTE_SIZE either (unless it's defined elsewhere that we don't see here), which the internal Cube's analyzer is likely to spot as an error.

Where is GHW_PALETTE_SIZE defined in the project?
« Last Edit: June 06, 2025, 03:50:36 pm by SiliconWizard »
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2570
  • Country: pl
Re: Please help with this C syntax error
« Reply #7 on: June 06, 2025, 06:09:34 pm »
And, I think we have a winner here. That would be the only missing part that I can spot as well.
Not the only one. It also doesn’t include any other header, despite at least “stdint.h” or “inttypes.h” are required for it to compile. So I assumed so far the code is incomplete. If GHW_PALETTE_SIZE was undefined, this would also be untrue: “it compiles without apparent errors.”

Which of course doesn’t mean you are wrong, and OP should check any hint. The reason is: if the code is incomplete, our picture of the issue is incomplete.

peter-h, when asking for help with code, it is good to minimal code, that shows symptoms, but otherwise compiles/works. This is not only for convenience of whoever replies. It is, above all, an extremely effective debugging and problem solving technique. The path you need to take to provide such an example snippet, in most cases leads you to a better understanding and the discovery what exactly breaks.

Since the code compiles, but (I assume) the editor reports an error, and under assumption that SiliconWizard is not right: many tools processing C are only supporting a C-ish language. Part guessed, part only in devs’ imagination. While inclusion of that file is valid in C and happens during the preprocessing stage, before “C proper” is even analyzed, the IDE may be doing some pattern matching instead. And that may fail with a less common construct.

An alternative explanation is a different paths configuration in the IDE and in the compiler. Includes with angle brackets must be in tool’s search path, unlike includes with quotation marks. Are both tools having the same configuration in that regard?
Why 📎 | We live in times when half of people have IQ below 100.
 
The following users thanked this post: gmb42

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6146
  • Country: gb
  • Doing electronics since the 1960s...
Re: Please help with this C syntax error
« Reply #8 on: June 06, 2025, 06:23:32 pm »
GHW_PALETTE_SIZE is defined. If it wasn't, it would not compile.

OK next time I will post more :)

Good point about the brackets. In my project they seem to be interchangeable i.e. everything is in the search path.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1936
  • Country: gb
Re: Please help with this C syntax error
« Reply #9 on: June 06, 2025, 06:25:41 pm »
If it compiles without issue, it could just be something dumb like the CubeIDE editor's syntax checker doesn't like/understand it, when it is in fact perfectly legitimate usage of the pre-processor.

Edit: I wonder if it's just the #include statement being followed immediately by just a semi-colon.
« Last Edit: June 06, 2025, 06:29:49 pm by HwAoRrDk »
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 13125
  • Country: us
Re: Please help with this C syntax error
« Reply #10 on: June 06, 2025, 07:13:51 pm »
A standard approach to this kind of problem is to make the C compiler output the result of the pre-processor to a separate file, and then examine that file to see if it is what you expect. If it looks OK, compile that file and examine the compiler diagnostics from that.

Note that in your opening post, you have not shown what the compiler diagnostic messages are?
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17841
  • Country: fr
Re: Please help with this C syntax error
« Reply #11 on: June 06, 2025, 08:37:55 pm »
If it compiles without issue, it could just be something dumb like the CubeIDE editor's syntax checker doesn't like/understand it, when it is in fact perfectly legitimate usage of the pre-processor.

It sure is that. Now that may have helped the OP if we could spot what makes it choke, but I think it's impossible without seeing more.

I do suspect that the IDE's internal analyzer doesn't properly preprocess the source code, at least not to the extent/with the options that the compiler does, which is something very common with those built-in analyzers. It can also happen with language servers in VS Code.

Keep in mind those analyzers are optimized so as not to take several seconds every time you type something, so they can't be as complete as the real deal. I wouldn't give them much attention.
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 4870
  • Country: us
Re: Please help with this C syntax error
« Reply #12 on: June 06, 2025, 08:41:59 pm »
This is 100% just an issue with the IDE syntax checker.  They are significantly different than compilers. They generally have to work much more locally to be fast and since it's expected the file has many errors they would prevent a traditional parse. I would expect that it does something like this.  The IDE maintains a table of each known header along with the definitions it creates.  When it sees an #include it adds the definitions, but doesn't actually process the include as text substitution.

This happens to also mirror how import statements work in languages that are not C/C++.
« Last Edit: June 06, 2025, 08:44:08 pm by ejeffrey »
 
The following users thanked this post: nctnico

Offline IanB

  • Super Contributor
  • ***
  • Posts: 13125
  • Country: us
Re: Please help with this C syntax error
« Reply #13 on: June 06, 2025, 08:56:42 pm »
It might be possible to overcome the IDE syntax confusion by adjusting the code as below, and then adjusting the include file accordingly. This would make the syntax look valid even if ignoring the contents of the include file:

Code: [Select]
#ifdef LCD_CONTROLLER_ST7789
static GCODE GPALETTE_RGB FCODE ghw_palette[GHW_PALETTE_SIZE] = {
#include <gcolor_4_ST7789.pal>
};
#endif
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3820
  • Country: us
Re: Please help with this C syntax error
« Reply #14 on: June 07, 2025, 02:22:14 am »
Comments are removed before preprocessor commands are followed. That means that the line
Code: [Select]
#define FCODE    /* nothing */
is syntactically equivalent to
Code: [Select]
#define FCODE     
because the comment is replaced by a single space before the macro definition is scanned. Since a macro is supposed to be defined as a sequence of tokens, this looks like a syntax error.

Is gcolor_4_ST7789.pal part of the IDE or part of your project? The angle-bracket syntax for file inclusion is for headers that are part of the system. Use the double-quote syntax for headers from the project.
« Last Edit: June 07, 2025, 02:29:09 am by helius »
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 13125
  • Country: us
Re: Please help with this C syntax error
« Reply #15 on: June 07, 2025, 02:58:26 am »
Comments are removed before preprocessor commands are followed. That means that the line
Code: [Select]
#define FCODE    /* nothing */
is syntactically equivalent to
Code: [Select]
#define FCODE     
because the comment is replaced by a single space before the macro definition is scanned. Since a macro is supposed to be defined as a sequence of tokens, this looks like a syntax error.

Is gcolor_4_ST7789.pal part of the IDE or part of your project? The angle-bracket syntax for file inclusion is for headers that are part of the system. Use the double-quote syntax for headers from the project.

Agreed, although I believe "#define FCODE" would define it as "nothing" and so it would be replaced with nothing (i.e. eliminated) in the source text.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Please help with this C syntax error
« Reply #16 on: June 07, 2025, 05:38:34 am »
This is what you are occasionally going to see with IDEs with their internal code parsers. They are not compilers; bug reports are not filed as eagerly as for actual compilers; they don't even try to guarantee compliance with whatever language they are parsing; they are just "helpful" semi-compilers that may or may not understand some parts of the language.

Sometimes you just need to ignore the errors and warnings they give and go on. Of course sometimes they error out in an awkward way, for example, refuse to work at all (offer suggestions, pop-ups etc) if they think there is "error" above the current position.

So to be clear, there is absolutely nothing wrong or unusual in your code. Perfectly valid, and often used pattern. Just, to quote yourself, CubeIDE being buggy piece of shit, like IDEs usually are. You just need to cope with that. It is up to you to choose:
* Trying random modifications on your code until it passes CubeIDE's buggy parser,
* Ignore the error if that does not prevent you from working
* Change IDE, but the situation is unlikely to go much better.
« Last Edit: June 07, 2025, 05:40:38 am by Siwastaja »
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6146
  • Country: gb
  • Doing electronics since the 1960s...
Re: Please help with this C syntax error
« Reply #17 on: June 07, 2025, 05:49:49 am »
Quote
Is gcolor_4_ST7789.pal part of the IDE or part of your project? The angle-bracket syntax for file inclusion is for headers that are part of the system. Use the double-quote syntax for headers from the project.

It is my project, but the file format is fixed due to the font/icon editor which the graphics library vendor (Ramtex) supplies. That bit of code is also in the graphics library.

Whether I will ever use that icon editor is another matter. It is an efficient way to do graphics for "instrument displays", however.

Yes I normally use double quotes in my code. Keep things simple :)
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Please help with this C syntax error
« Reply #18 on: June 07, 2025, 08:50:00 am »
Code: [Select]
#define FCODE     
Since a macro is supposed to be defined as a sequence of tokens, this looks like a syntax error.

No, #define FCODE is not only valid, it's also one of the most commonly used preprocessor patterns in nearly every real-world code base, like this:
#define FEATURE

...

#ifdef FEATURE
...
#endif


It is beyond imagination that any IDE, tool or parser would fail on that.
 
The following users thanked this post: newbrain

Offline dietert1

  • Super Contributor
  • ***
  • Posts: 3042
  • Country: br
    • CADT Homepage
Re: Please help with this C syntax error
« Reply #19 on: June 07, 2025, 09:10:38 am »
It's pretty easy to understand: The syntax checker goes by file. That #include to pull in the color data results in unexpected/unknown syntax, and it's a reasonable first result to qualify that as bad syntax. The include file itself is also invalid syntax, in case you check it.

Regards, Dieter
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Please help with this C syntax error
« Reply #20 on: June 07, 2025, 09:46:54 am »
That #include to pull in the color data results in unexpected/unknown syntax

Where exactly? I see no problems with it.
 

Offline dietert1

  • Super Contributor
  • ***
  • Posts: 3042
  • Country: br
    • CADT Homepage
Re: Please help with this C syntax error
« Reply #21 on: June 07, 2025, 10:37:51 am »
It doesn't read the include file, which can have anything.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Please help with this C syntax error
« Reply #22 on: June 07, 2025, 01:03:34 pm »
It doesn't read the include file, which can have anything.

Oh yeah. And it should be reading the file, that is literally the only meaning of #include. Because it does not, it breaks down. It's of course a "performance" optimization, but that's only an excuse - a modern compiler eats megabytes of code in seconds and not only parse and tokenize it correctly, also create optimized machine code in that time. An IDE does not need to do such compilation on every keypress - it could well assign some time in background to read that file once it changes and tokenize it then.

But as it fails to do, it breaks on code which has nothing wrong. A bug report could be filed, but will likely not lead anywhere. This is how life is with IDEs: their code parsers are small partial subset of the relevant languages, and there is not enough resourcing on the projects to make them better. We just need to ignore their unnecessary warnings and trust the compilers instead.
« Last Edit: June 07, 2025, 01:06:40 pm by Siwastaja »
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 30418
  • Country: nl
    • NCT Developments
Re: Please help with this C syntax error
« Reply #23 on: June 07, 2025, 01:48:37 pm »

In Cube IDE I get an undefined "syntax error" in the editor, but it compiles without apparent errors. Can't tell if it runs because I am days away from getting to a buildable state (it is a commercial graphics library; a huge mess of very similar files for different LCD controllers).

I thought the "#define FCODE    /* nothing */" is a problem but removing the FCODE makes no difference.

The syntax error is showing on the three lines starting with static GCODE GPALETTE_...
If it is not the syntax checker wanting to process complete C files only, then most likely the include path is not added to the project in a way the IDE's syntax checker can find it and thus it gives a parsing error. The typical fix is to add the path where the include is to the project settings (under paths & symbols).
« Last Edit: June 07, 2025, 02:08:11 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6146
  • Country: gb
  • Doing electronics since the 1960s...
Re: Please help with this C syntax error
« Reply #24 on: June 08, 2025, 06:56:20 am »
i don't know where one might set up a path given that the compiler and linker etc find all the files just fine in the project paths already set up.

But sure it is a possibility to specify the full path to the include file in the editor. So I tried it; no difference.

Had that .pal file been structured to contain the whole valid C syntax, it would have been OK

Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->