Connect with us

How to implement "syntax sensing"

Discussion in 'Microcontrollers, Programming and IoT' started by Solidus, Oct 31, 2013.

Scroll to continue with content
  1. Solidus


    Jun 19, 2011
    Alright guys, I have one hell of a project I'd like to make a few stabs at, and I'd much appreciate it if any of you had any help you could give me in various parts.

    As some of you know, I work on developing iPhone app, and am right now in the midst of publishing the first one. I'm also a firm believer in that the best way to learn something is to force myself out of the comfort zone ("dive in", if you will), and it's one thing to build simple calculator apps or manipulate strings, characters, primitives, etc.

    It's another thing to actually handle with data.

    Now as soon as my first is published, I don't want to sit around and get the feeling of being content. Learning programming is one of my favorite things to do (social life :p), and I don't want to sit around idling with that knowledge.

    Long story short, I want to implement an AVR/Arduino simulator into an iPhone app. Not a compiler/IDE, but something that would ideally allow someone to upload a sketch file/open one/write one and that would provide a visual set of references as to how it would accurately handle, effectively 'simulating' that code in the virtual environment of an iPhone.

    Now, Objective-C isn't completely new to me, and I have a fair amount of knowledge as to how to handle in it. Problem is, I don't know how to implement 'syntax sensing', or, for lack of a better word, how to detect when something in the code is called.

    For anyone with experience, obviously Obj-C is powerful enough and carries filestream functions that can open files and read, write to, and parse them, as well as provide line detection and delineation. A 'foolproof' way to do this would be to use if/statements and scan the entire file for all known keywords (i.e. int, string, for, etc.), but even in a 'beginner-friendly' language such as the Arduino adaptation of Wiring, that's a lot of possibilities.

    Any ideas? I feel that if I could pull this off with finesse and make a good job out of it, it would not only help a lot of people on this forum constantly asking AVR questions, but a lot of others as well.
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    Nov 17, 2011
    From my point of view this is not a simple task you are about to tackle.
    Let me start by noting that I don't know the particular language called objective V, but I know C and a little bit of C++.
    These languages are not designed as interpretative languages. But interpret is what you are going to do - as it sounds to me. Obviously it is possible: Here is a tutorial on rolling your own C-interpreter. You probably can use this as a basis to be extended to other C-variants.

    You may, however, be better off by having the source code compiled on a PC an interpret the compiled code. This way the compiler will have done all the syntax checking, lexical analysis etc. and give you a program that's at least syntactically correct machine code (still doesn't mean it does what you want :) ). In that case your machine code interpreter does not have to look for correct syntax but can concentrate on simulating the execution of the instructions one by one. If you include debug information into the machine code, you are also able to trace the execution of the machine code on the source code level.

    To get an idea what I'm talking about have a look at the AVR-studio. It is a free IDE for Atmel processors. Using WinAVR as a plugin you can code in C and debug on the machine code level - or C, if you so wish. Running a lengthy program in the simulator is rather slow, even on a fast PC. I wonder how much slower this would run on a smartphone?

    Or maybe I have interpreted (pun intended) your request completely wrong?



    P.S.: Here is another tutorial on building a compiler. You may want to study the techniques used to analyse the input and generate a machine readable output. The same techniques may come in handy for your project.
    Last edited: Oct 31, 2013
  3. Solidus


    Jun 19, 2011

    Objective-C is a "front-end" to C that gives it object-oriented support. That is good news; it means that all C constructs will work in Obj-C by default.

    Maybe with my original wording I was aiming a bit too 'broad-side of barn'. What I'm looking to do is have more of an I/O simulator. Something that could look at and interpret the code, but to a cursory enough extent and with the goal of simulating I/O and returning basic information such as serial messages and timing info on the I/O.

    Does this sound more feasible?

    And thanks for the information on building the compiler! That will be a lot of fun to look through, if for nothing else but my side projects.
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    Nov 17, 2011
    Yes and no. I think it is feasible to simulate the I/O behavior of a program. As I mentioned, AVR studio does this.
    But I doubt it is possible to a "cursory extent". You want the timing, so you need to go very low level in your simulation. To do this, you need to know how the high level code translates to machine code.
    Another approach would be to model the behavior (including timing) of a piece of code, e.g. build a model of a subroutine. Your model would represent the behavior of the respective piece of hardware when this subroutine (method in object-speech) is called. This can be done using much less ressources of your computer/tablet than simulating the machine code step by step. This is definitely feasible since it has been done already 20 years ago.
    A big caveat: You will have to create a model for each piece of code you want to simulate. Maybe this can be done automatically, but then we are back to building sort of a compiler again.
    Last edited: Nov 1, 2013
  5. Solidus


    Jun 19, 2011
    My apologies for taking half a month to get back to you - had some things that consumed too much of my time in the meantime.

    I see what you're saying - it won't be too hard to get timing information off of pieces of code, provided we know the cycle timing from the architecture datasheets. But, I do understand what you are saying about it needing a compiler, as it does have to be determined what the code structure is. It wouldn't be difficult to code a generic 'for' loop setup, but what if the for loop is nested in an if statement?

    That's when we need the syntax tree.

    I'll do some more looking and get back to you.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day