4 Replies - 4036 Views - Last Post: 07 February 2014 - 10:35 PM

#1 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10363
  • View blog
  • Posts: 38,355
  • Joined: 27-December 08

[Link] So You Want to Write Your Own Language

Post icon  Posted 05 February 2014 - 07:22 AM

A good article for those who are curious about what it takes to design one's own programming language. A small excerpt below:

Quote

Things that are true gods:

1. Context-free grammars. What this really means is the code should be parsable without having to look things up in a symbol table. C++ is famously not a context-free grammar. A context-free grammar, besides making things a lot simpler, means that IDEs can do syntax highlighting without integrating most of a compiler front end. As a result, third-party tools become much more likely to exist.

2. Redundancy. Yes, the grammar should be redundant. You've all heard people say that statement terminating ; are not necessary because the compiler can figure it out. That's true but such non-redundancy makes for incomprehensible error messages. Consider a syntax with no redundancy: Any random sequence of characters would then be a valid program. No error messages are even possible. A good syntax needs redundancy in order to diagnose errors and give good error messages.

3. Tried and true. Absent a very strong reason, it's best to stick with tried and true grammatical forms for familiar constructs. It really cuts the learning curve for the language and will increase adoption rates. Think of how people will hate the language if it swaps the operator precedence of + and *. Save the divergence for features not generally seen before, which also signals the user that this is new.


http://www.drdobbs.c...guage/240165488

Is This A Good Question/Topic? 2
  • +

Replies To: [Link] So You Want to Write Your Own Language

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8891
  • View blog
  • Posts: 33,338
  • Joined: 12-June 08

Re: [Link] So You Want to Write Your Own Language

Posted 05 February 2014 - 07:53 AM

Good find!
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2085
  • View blog
  • Posts: 3,170
  • Joined: 21-June 11

Re: [Link] So You Want to Write Your Own Language

Posted 05 February 2014 - 08:43 AM

View Postmacosxnerd101, on 05 February 2014 - 03:22 PM, said:

IDEs can do syntax highlighting without integrating most of a compiler front end


Sure, but they still can't do intelligent auto-completion or error detection without "integrating most of a compiler front end", so what's the point? I'd say offering a good API for your compiler makes the IDE's job much easier than having a simple grammar. Which isn't to say that you can't have both, of course.

Quote

Consider a syntax with no redundancy: Any random sequence of characters would then be a valid program. No error messages are even possible. A good syntax needs redundancy in order to diagnose errors and give good error messages.


Suggesting that all, or even most, diagnosable errors are syntax errors?
Was This Post Helpful? 1
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: [Link] So You Want to Write Your Own Language

Posted 07 February 2014 - 09:22 AM

The whole redundancy thing is a bunch of hot gas. He makes no actual point. I'm not agreeing or disagreeing with it; I'm saying it is just a bunch of non-sense. Something of a straw man if you ask me. Reading further in the thing he makes some decent points. I quite like his points on lowering and error messages.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: [Link] So You Want to Write Your Own Language

Posted 07 February 2014 - 10:35 PM

<sarcasm>
Yes. Redundancy is good. Forget about DRY principles. If the code was good enough to write once, it must be good to write it again a few more times.

class Foo
{
public:
    Foo();

    int DoSomething(const std::string & input);

private:
    std::vector<std::string> m_entries;
#ifdef DEBUG
    static int m_instanceCounter;
#endif
}

#ifdef DEBUG
int Foo::m_instanceCounter = 0;
#endif

Foo::Foo()
{
    m_instanceCounter++;
}

int Foo::DoSomething(const std::string & input)
{
    for(std::vector<string>::iterator iter = m_entries.begin();
        iter != m_entries.end();
        iter++)
    {
        if (input == *iter)
        {
            return iter - m_entries.begin();
        }
    }

    m_entries.push_back(input);
    return -1;
}



</sarcasm>

For those who don't read C++, notice that I declared a class static variable that is only supposed to be used for DEBUG builds on lines 10-12, but I have to repeat the definition on lines 15-17. Notice the constructor declarations on lines 4 and 6 that need to be repeated on lines 19 and 24. Notice the iterator declaration on line 26 with all that repetition of something that could be derived from the the right hand side. (Blessings on whomever introduced the auto keyword, as well as the ranged based for loops.]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1