CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

JSON decoder
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

JSON decoder
PostPosted: Wed Nov 16, 2022 2:46 pm     Reply with quote

Hello,

I'm trying to run this JSON decoder ( http://ww1.microchip.com/downloads/en/Appnotes/90003239A.pdf ) with CCS compiler, but have no success yet.

Code:


#include <16F1459.h>
#device *=16
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO,NOPLLEN,NOPROTECT,MCLR,PUT,NOWDT  // CLKOUT -> Enable clk/4 out on PIN_A4
#use delay(int=16MHz, clock=24MHz)
#use RS232(DEBUGGER,rcv=PIN_B6,xmit=PIN_B6,stream=STREAM_SERIAL_INPUT)

#include "json_decoder.c"
#include "mem_model.c"
#include "lexer.c"
#include "parser.c"

#define TEST_JSON "{\"main\":{\"key\" : 10,\"foo\":\"bar\"}, \"alt\":2}"

char str[64];
jsonNode_t *root = 0;
jsonDecoderStatus_t ret;

void main(){

   memcpy(str, TEST_JSON, sizeof(TEST_JSON));
   ret = JSON_DECODER_fromString(str);   
   
   if(JSON_DECODER_OK != ret)
   {
      printf("Invalid JSON string.\r\n");
   } 
   
   JSON_DECODER_getRoot(&root);
   
   while(1){
   // user code ...
   }
}



CCS compiler(v.5.092) return so many errors:


>>> Warning 209 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\lexer.c" Line 106(1,1): Assignment to enum is not of the correct type
>>> Warning 209 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\lexer.c" Line 116(1,1): Assignment to enum is not of the correct type
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 77(187,188): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 78(196,197): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 81(245,246): A numeric expression must appear here
*** Error 27 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 87(91,92): Expression must evaluate to a constant
*** Error 12 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 89(108,111): Undefined identifier pop
*** Error 22 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 90(125,126): Bad expression syntax
*** Error 12 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 93(209,212): Undefined identifier pop
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 117(254,255): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 118(24,25): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 121(126,127): A numeric expression must appear here
*** Error 22 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 131(88,89): Bad expression syntax
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 152(201,202): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 153(222,223): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 156(58,59): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 161(221,222): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 162(242,243): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 165(71,72): A numeric expression must appear here
*** Error 22 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 169(141,142): Bad expression syntax
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 176(101,105): A numeric expression must appear here
*** Error 12 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 194(157,158): Undefined identifier pop
*** Error 12 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 194(157,158): Undefined identifier pop
*** Error 12 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 195(189,192): Undefined identifier pop
*** Error 12 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 196(240,243): Undefined identifier pop
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 201(147,148): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 202(168,169): A numeric expression must appear here
*** Error 51 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 207(237,244): A numeric expression must appear here
*** Error 43 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 212(29,35): Expecting a declaration
*** Error 48 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 212(36,42): Expecting a (
*** Error 43 "D:\MCU Archives\pic my project\pic my project\16F1459_USB_projects\JsonDecoder\parser.c" Line 213(43,44): Expecting a declaration



Can anyone help?

Best Regards,
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Nov 16, 2022 3:37 pm     Reply with quote

well 1st 2 errors are in the 'lexer.c' chunk of code, the rest are from 'parser.c'
You'd have to post those for anyone here to decide what is wrong.

it's probably your conversion to CCS C that's causing the errors as different compilers defaults are never the same.....

BTW...the PDF article doesn't have any source code that I could see
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Wed Nov 16, 2022 10:51 pm     Reply with quote

temtronic wrote:


BTW...the PDF article doesn't have any source code that I could see


Hi,
There is a link to github on the first page.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Nov 17, 2022 5:04 am     Reply with quote

First thing to use MicroChip code, you almost certainly need to set
#DEVICE ANSI
Microchip by default has all it's integers as signed. CCS does not.

Then every reference to 'int' will need to change to 'int16'.

Now the line that fails in parser, in the original file is a blank line. Implies
you must have edited the file, so what we are looking at is not what you
are using.
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Thu Nov 17, 2022 2:18 pm     Reply with quote

Hi,

The "#DEVICE ANSI" directive and int to int16 changed, but has no idea for this:
Code:

parserStatus_t PARSER_parseTokens(void)
{
    parserStatus_t retVal = PARSER_OK;

    jsonToken_t nextToken;
    jsonNode_t *currentNode = NULL;
    jsonNode_t *previousNode = NULL;

    ASSERT_AVAILABLE_SOFT_STACK;
    STACK[stackTop++] = (recursionStack_t){
        .type = CALL,
        .data =
        {
            .root = 1,
        }
    };

    while (stackTop > 0)
    {
        recursionStack_t pop = STACK[--stackTop];

        switch (pop.type)
        {
            case CALL:
                ASSERT_TOKEN(&nextToken);
                if (pop.data.root && nextToken.type != LEFT_BRACE)
                {
                    retVal = PARSER_BAD_FORMAT; //Root must be an object
                    continue;
                }

                if (nextToken.type == LEFT_BRACE)
                {
                    ASSERT_NODE_ALLOC(currentNode);

                    if (previousNode != NULL)
                    {
                        previousNode->type = OBJECT;
                        previousNode->v = currentNode;
                    }

                    ASSERT_TOKEN(&nextToken);
                    if (RIGHT_BRACE == nextToken.type)
                    {
                        retVal = PARSER_OK;
                        continue;
                    }

                    ASSERT_AVAILABLE_SOFT_STACK;
                    STACK[stackTop++] = (recursionStack_t){
                        .type = CALL_LOOP,
                        .data =
                        {
                            .root = 0,
                        }
                    };
                }
                else
                {
                    retVal = LEAF_TOKEN;
                    continue;
                }

                break;

            case CALL_LOOP:

                if (STRING != nextToken.type)
                {
                    retVal = PARSER_BAD_FORMAT; // key must be string
                    continue;
                }
                currentNode->key = nextToken.value.str;

                ASSERT_TOKEN(&nextToken);
                if (COLON != nextToken.type)
                {
                    retVal = PARSER_BAD_FORMAT; // expecting colon after key
                    continue;
                }

                previousNode = currentNode;

                ASSERT_AVAILABLE_SOFT_STACK;
                STACK[stackTop++] = (recursionStack_t){
                    .type = RESUME_LOOP,
                    .data =
                    {
                        .currentNode = currentNode
                    }
                };

                ASSERT_AVAILABLE_SOFT_STACK;
                STACK[stackTop++] = (recursionStack_t){
                    .type = CALL,
                    .data =
                    {
                        .root = 0,
                    }
                };

                break;

            case RESUME_LOOP:
                if (retVal == LEAF_TOKEN)
                {
                    memcpy(&currentNode->value, &nextToken.value, sizeof (currentNode->value));
                }
                else if (retVal != PARSER_OK)
                {
                    retVal = PARSER_BAD_FORMAT;
                    continue;
                }

                ASSERT_TOKEN(&nextToken);
                if (RIGHT_BRACE == nextToken.type)
                {
                    retVal = PARSER_OK;
                    continue;
                }
                else if (COMMA != nextToken.type)
                {
                    retVal = PARSER_BAD_FORMAT; // expecting comma
                    continue;
                }

                ASSERT_NODE_ALLOC(pop.data.currentNode->h);
                previousNode = pop.data.currentNode;
                currentNode = pop.data.currentNode->h;

                ASSERT_TOKEN(&nextToken);

                ASSERT_AVAILABLE_SOFT_STACK;
                STACK[stackTop++] = (recursionStack_t){
                    .type = CALL_LOOP
                };

                break;

            default:
                break;
        }
    }

    return retVal;
}


Microchip published appnote and source code that doesn't work? ? Sad
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 17, 2022 3:21 pm     Reply with quote

It's highly unlikely the code presented by Microchip doesn't work BUT you have to run it on whatever PIC they used and compiler...

I've been caught on the 'device=ans' a couple of times the past, though you've fixed that...
Also check the other 'data definitions or defaults but you should post what errors you're getting.
It could be in your translation into CCS C you've mistyped something, changes a : into a ; or some similar typing error

It's very difficult to 'remotely diagnose what the actual cause of the problem is. For me it's 99.44% a typing error, wished I'd taken 'touch typing' in high school, 1/2 a century+ ago...
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Thu Nov 17, 2022 3:55 pm     Reply with quote

Hi mr. temtronic,

Quote:

For me it's 99.44% a typing error


It's not my typing error. I've just downloaded 'working' code from GitHub link and edited as mr. Ttelmah point to int vars. to int16.
You can also download .c and .h files and run my first posted example code.

Best Regards,
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 17, 2022 4:06 pm     Reply with quote

hmm, there's no 'main.c' in the GitHub link, so no reference as to what PIC was used... Or it might have been an AVR ... Also no mention of which C compiler was used to create the code.
As such, the document is not complete and I'm unable to 'copy/paste/compile/test'.
Others who do this for a living ( I'm retired..) may be able to 'fill in the blanks' and create a workable program ( main.c).
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Nov 18, 2022 2:15 am     Reply with quote

That is because you are including parser.h, after parser.c is being loaded.
The .h files must be included _first_. The compiler does not know what
PARSER_BAD_FORMAT actually 'is'.

Seriously, the code will only be 90% 'compatible'. Everything about
the actual I/O will need to be customised to suit CCS. The ordering of
the files, the declaration of the variables, etc. etc..
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Nov 18, 2022 6:39 am     Reply with quote

What's curious to me, is that in the original github files, the first line in the parser.c file IS the #include for the parser.h file (same format for the others BTW...)
Puzzling is WHY even have the parser.h file, as it only contains 5-6 lines of code which you need for parser.c, so why not just put them IN the parser.c file ?

Without seeing the complete 'picture' it's hard to diagnose why the failure.
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Sat Nov 19, 2022 11:42 am     Reply with quote

Ttelmah wrote:
That is because you are including parser.h, after parser.c is being loaded.
The .h files must be included _first_. The compiler does not know what
PARSER_BAD_FORMAT actually 'is'.


Hi mr.Ttelmah,
The "parser.h" file it's included in "parser.c". Example for this app.note says that use " ATmega4808" which is 8bit AVR and I think they use MPLAB XC8 compiler.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sun Nov 20, 2022 10:34 am     Reply with quote

You are missing the fundamental point.
You can't just take code from one compiler and expect it to compile and
run on another. Though the core 'C' functions are 99% portable, everything
handling I/O, is compiler specific, and has to be adapted by _you_ to
work with a different compiler.
You need to go through the code and work out which lines handle I/O,
and re-code these using the CCS functions. The same applies when moving
from a Raspberry Pi to Microchip, or AVR to PIC.
Code is only 90% portable, not 100%.
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Sun Nov 20, 2022 1:36 pm     Reply with quote

Hi,
Okay mr.Ttelmah,
But in this case there is no specific periphery just ‘standard’ c code and one test string.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Nov 20, 2022 2:04 pm     Reply with quote

hmm... 'standard' C....
No such thing, several variations of 'C" exist, each compiler has it's own 'set of C'..and DEFAULTS

I believe CCS C is closely based on K&R version of C, though newer versions have added more 'features/functions'...

That's why Mr. T keeps saying you have to read the CCS manual, look for differences in the GitHib posted C code, which actually isn't posted as to WHAT compiler it is.....
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Sun Nov 20, 2022 2:35 pm     Reply with quote

Hi,
I have checked all vars. and types and needs to edit just two int to int16 all others are ok and using ccs’s <stdint.h>.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group