News for hackers. Stuff that matters.
Re:The naked truth about writing a programming language (2014) by billconan on Saturday May 02, 2020 @02:50PM (#23053972)
I want to implement a toy programming language, but I have questions regarding the following in that article:

> Context free grammars. What this really means is the code should be parseable 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 in most of a compiler front end, i.e. third party tools become much more likely to exist.

Many complex languages, like c++, rust are not context free. We therefore need semantic analysis.

What programming language features will be compromised if I stick to context free?

Will the end result be as expressive as c++/rust?

Any programming language that is massively adopted is context free?

BTW, the toy language I want to build will be something similar to javascript.

Re:The naked truth about writing a programming language (2014) by kyllo on Saturday May 02, 2020 @09:17PM (#23056150)
Context-free has nothing to do with symbol tables, it just means that in the grammar, the left-hand side of a production rule can only have a single non-terminal symbol, which can always be replaced by the expression on the right-hand side, without ambiguity.

Language features are an orthogonal issue--you can implement any language feature with a CFG, but you just can't reuse the same keyword or operator to have different meanings in different grammatical contexts.

A classic example is that in C++ it is impossible to know whether << is brackets around template arguments, or the left bit shift operator, or the stream output operator, without arbitrary lookahead--you need to parse the rest of the statement in order to decide which one it is in the current statement's context.

Generally language designers don't choose to implement a context-sensitive grammar on purpose because they desire some feature that requires it; they strive for a context-free grammar, but end up with a handful of special cases that require context-sensitive parsing because of either convenience or legacy reasons.

Popular programming languages nearly all use custom parsers written in other (or the same) Turing-complete programming languages, so it's not "harder" to parse the context-sensitive rules, it only poses a problem if you want to use a parser generator to implement parsing for a CSG.

Re:The naked truth about writing a programming language (2014) by johannes1234321 on Saturday May 02, 2020 @10:33PM (#23056445)
> A classic example is that in C++ it is impossible to know whether << is brackets around template arguments, or the left bit shift operator, or the stream output operator, without arbitrary lookahead--you need to parse the rest of the statement in order to decide which one it is in the current statement's context.

The classic example, I guess, is the most vexing parse: https://en.m.wikipedia.org/wiki/Most_vexing_parse

"""

The line

  TimeKeeper time_keeper(Timer());
is seemingly ambiguous, since it could be interpreted either as

    a variable definition [...]
    a function declaration [...]
"""

These things you can only resolve by having the symbol tables around and checking. The parser alone can't tell.

A language could avoid this i.e. by requiring specific keywords or having more distinct syntax.

Humans (programmers) however often can deal with ambiguity, we have it in humannlanaguage as well and context on most cases helps and I argue those aren't practical problems. (While that's only opinion)

Re:The naked truth about writing a programming language (2014) by kyllo on Sunday May 03, 2020 @06:22PM (#23062368)
Yes and programming languages are human interfaces to machine instructions, so context sensitivity can be manageable and even desirable to human users of the language, even if it makes the implementation of the language interpreter more complex or less elegant. Programming language designers make this trade-off all the time.
Re:The naked truth about writing a programming language (2014) by bmn__ on Sunday May 03, 2020 @06:54AM (#23058227)
> without ambiguity

This is not a distinguishing feature. (I'm assuming you meant something else.) Example of a CFG with ambiguity:

    S → S S
    S → x
Re:The naked truth about writing a programming language (2014) by kyllo on Sunday May 03, 2020 @06:11PM (#23062274)
You're right, I meant that each valid RHS matches one and only one LHS--but that's also true of CSGs.
Re:The naked truth about writing a programming language (2014) by signaru on Sunday May 03, 2020 @03:33AM (#23057488)
In BASIC the equal symbol "=" can mean assignment or equality depending on "context". Is this an example of a non context free?

On the other hand, I've seen EBNF definition of BASIC (VB, actually). Is this a contradiction?

(just someone still learning)

Re:The naked truth about writing a programming language (2014) by kyllo on Sunday May 03, 2020 @06:17PM (#23062325)
It's still context-free, the reason is because by the time you hit the '=' symbol you already know whether you're in a <Statement> or a <Compare Exp> production rule, based on the preceding symbols (namely the LET keyword), based on these excerpts from the BASIC EBNF:

    <Statement>   ::= CLOSE '#' Integer
                    | DATA <Constant List> 
                    [...]    
                    | LET Id '=' <Expression> 
                    [...]
                    | Remark

    <Compare Exp> ::= <Add Exp> '='  <Compare Exp> 
                    | <Add Exp> '<>' <Compare Exp> 
                    | <Add Exp> '><' <Compare Exp> 
                    | <Add Exp> '>'  <Compare Exp> 
                    | <Add Exp> '>=' <Compare Exp> 
                    | <Add Exp> '<'  <Compare Exp> 
                    | <Add Exp> '<=' <Compare Exp> 
                    | <Add Exp>
Re:The naked truth about writing a programming language (2014) by foldr on Sunday May 03, 2020 @04:38AM (#23057745)
A context free grammar can handle this fine - as you can see from the EBNF - so no, it's not an example.
Re:The naked truth about writing a programming language (2014) by a1369209993 on Saturday May 02, 2020 @09:25PM (#23056175)
Note that "context free grammars", in the technical sense, is completely the wrong thing. Eg, enforcing consistent indentation is not context free:

  foo()
      {
      return;
      }
  ↓↓↓↓↓↓↓↓↓↓↓
  aaaa{
  bbbbreturn;
  cccc}
  ↓↓↓↓↓↓↓↓↓↓↓
  aaaabbbbcccc
This is equivalent to aⁿbⁿcⁿ, which is pretty much the canonical example of a non-context-free language.

What you acually want is not context-free grammar, but, as TFA says:

> the code should be parseable without having to look things up in a symbol table

Re:The naked truth about writing a programming language (2014) by qznc on Sunday May 03, 2020 @02:45AM (#23057332)
There is a trick for indentation: Use a context-sensitive lexer in front of a context-free parser. The lexer serves indent and dedent tokens to the parser.

With this trick, you can parse Python with a context-free grammar.

Re:The naked truth about writing a programming language (2014) by bmn__ on Sunday May 03, 2020 @06:56AM (#23058234)
There are plenty, the majority even, of programming languages that do not enforce consistent indentation. You make a false generalisation when you claim that CFG are completely the wrong thing.
Re:The naked truth about writing a programming language (2014) by gbear605 on Sunday May 03, 2020 @09:25AM (#23058838)
I'd suggest that generally the grammar should be context-free, but that a few features = can be non-context-free. Formatting like this should specifically be a separate check IMO.
Re:The naked truth about writing a programming language (2014) by tom_mellior on Sunday May 03, 2020 @08:11AM (#23058532)
> This is equivalent to aⁿbⁿcⁿ

Is it? You only indent with spaces, so a = b = c = ' ', and the problem you claim disappears.

Re:The naked truth about writing a programming language (2014) by gbear605 on Sunday May 03, 2020 @09:24AM (#23058827)
I'm not sure that's a solution. The problem is verifying that the three 'n's are equal, not that 'a', 'b', and 'c' are equal. Solving this cannot be done in a context free grammar [0].

[0]: https://cs.stackexchange.com/questions/33153/is-an-bn-cn-con...

Re:The naked truth about writing a programming language (2014) by tom_mellior on Sunday May 03, 2020 @10:41AM (#23059273)
Yeah, you're right. I got confused by the original phrasing.
Re:The naked truth about writing a programming language (2014) by desc on Saturday May 02, 2020 @04:00PM (#23054459)
https://stackoverflow.com/questions/898489/what-programming-...

Most languages have context-free syntax, which is what the article refers too. There really is no reason to sacrifice that. Even modern PHP recognises the value of having a parse tree independent of an entire compiler.

Context-free semantics is an entirely different matter, and I'm not even sure what it'd mean...

Re:The naked truth about writing a programming language (2014) by chubot on Saturday May 02, 2020 @04:16PM (#23054575)
The two top answers are in conflict. The second answer with 43 points is closer to right:

There are hardly any real-world programming languages that are context-free in any meaning of the word.

The first answer with 41 points is totally wrong: The set of programs that are syntactically correct is context-free for almost all languages

-----

A better source is this whole series by Trevor Jim, which has nice ways of relating theory to practice. He lists a bunch of reasons why you could consider nearly all programming languages not context-free.

http://trevorjim.com/parsing-not-solved/ -- hundreds of parser generators support context-free grammars, but there are almost no context-free languages in practice.

http://trevorjim.com/python-is-not-context-free/ -- A main point here is that you have to consider the lexer and parser separately. Grammars don't address this distinction, which arises in essentially all programming languages. Also there is some lexical feedback, similar in spirit to C's lexer hack.

http://trevorjim.com/haskell-is-not-context-free/

http://trevorjim.com/how-to-prove-that-a-programming-languag...

http://trevorjim.com/c-and-cplusplus-are-not-context-free/ -- the way LALR(1) conflicts are resolved in practice can make a language not context-free

(copying from a recent comment)

Re:The naked truth about writing a programming language (2014) by WalterBright on Saturday May 02, 2020 @04:36PM (#23054712)
The implementation of D has a lexer that is independent of the parser, and a parser that is independent of the rest of the implementation. I have resisted enhancement proposals that would put holes in those walls.
Re:The naked truth about writing a programming language (2014) by billconan on Saturday May 02, 2020 @06:34PM (#23055423)
could you give an example for

> Any programming language that is massively adopted is context free?

I want to have an intuition on what is context free and what is not.

Is D context free?

I mean I want to make a useful language, is being context free practical?

Re:The naked truth about writing a programming language (2014) by WalterBright on Saturday May 02, 2020 @06:52PM (#23055512)
D is context free in that the parser does not require semantic analysis in order to complete its parse.
Re:The naked truth about writing a programming language (2014) by cxr on Sunday May 03, 2020 @09:45AM (#23058942)
The best way would probably be to work on a language that isn't context-free. When you run into the problems context-sensitivity introduces, then you'll really start to understand it.

In the past, Pascal was widely used in teaching contexts, for both learning to program and later how to implement a compiler. I've seen Walter say in the past that he got into doing languages from a series of articles in Byte magazine that included the listings for a small Pascal compiler. For your task, you can use Oberon instead, which is a smaller and simpler than Pascal. Oberon-07 is context free up to a point, which is another way of saying that it's not context-free. It'll work well for this exercise. Don't worry about writing a full-fledged compiler, because you'd end up spending forever on the code generator backend. Instead implement half of a compiler.

Here's one project that fits the description of being one half of a compiler—a documentation generator:

https://github.com/schierlm/OberonXref/

Implementing this requires doing the lexer and the parser, but its "backend" streams out documentation pages instead of object files or assembler. The truth is, you wouldn't even need to go that far. Just do the lexer and then start implementing the parser—let's say that your program should just read in source code and then say whether it's valid or not—whether it will parse. Since Oberon is mostly context-free and the natural way to do things is to use a recursive descent parser, you'll get into a rhythm implementing things, until you run into the part that's context-sensitive, at which point things will get uncomfortable. You will find true enlightenment then. This can be done over a weekend, but if you reach the end of the first weekend and realize it will take you two, don't sweat it.

> I mean I want to make a useful language, is being context free practical?

It is. If you do the exercise above, it should be straightforward to figure out several different ways to change the grammar to eliminate context-sensitivity in your parser. You'll realize that the transformations would be minor. They won't compromise the power or practicality of the language, and it's possible to end up with one that is almost exactly the same. The biggest casualty in this case would be reworking a dubious design decision made for aesthetic reasons.

Re:The naked truth about writing a programming language (2014) by billconan on Saturday May 02, 2020 @06:52PM (#23055509)
Thank you very much for the references. They are very good reads!

In the article about Python, the author said:

> Most languages of interest are context sensitive, and yet we are trying to use inappropriate tools (context-free grammars and context-free parser generators) to specify and parse them. This leads to bad specifications and bad implementations.

Then what do you think is a better tool to specify and parse languages?

Re:The naked truth about writing a programming language (2014) by chubot on Sunday May 03, 2020 @02:27AM (#23057280)
what do you think is a better tool to specify and parse languages?

That's unfortunately an open problem. As I mentioned here, lexing is "solved" but parsing isn't:

https://github.com/oilshell/oil/wiki/Why-Lexing-and-Parsing-...

Here are what some recent languages do:

Go: Maintain a written spec [1], with multiple hand-written parsers. (I think the main one is in Go, while gccgo has one in C++?) I believe Go used to be parsed with yacc, but they moved to a hand-written parser for error messages. It looks like [2] was an early attempt to keep it generated.

[1] https://golang.org/ref/spec

[2] https://research.swtch.com/yyerror

-----

Rust: The parser is hand-written, like Go. There's a grammar in the spec, but like Go's, it's not used to generate code, so it isn't tested. They also have an LALR(1) grammar which was suppoed to be an executable specification, but as far as I can tell that effort has not made progress recently.

It used to be done with GNU yacc but it looks like they moved to a Rust-based tool [3]

[1] https://doc.rust-lang.org/grammar.html

[2] https://github.com/rust-lang/lang-team/tree/master/working-g...

[3] https://github.com/rust-lang/wg-grammar/tree/master/grammar

-----

So basically the state of the art is fairly laborious. It's basically write down the grammar, implement it by hand, and try not to make any mistakes. If you need to implement it again, which is extremely common (e.g. for gccgo, for Rust's language server), also try not to make any mistakes.

When you need to change the language, update the spec, and change all the parsers by hand.

I've written multiple recursive-descent parsers based on grammars, and it's not hard if you have examples to test with, but mistakes can easily creep in.

Rely on users to report bugs. There is a "long tail" of programs that will tickle various corner cases.

-----

Here's the approach I took with Oil:

How to Parse Shell Like a Programming Language http://www.oilshell.org/blog/2019/02/07.html

which is basically to do as much work as possible in the lexer, use a high-level programming language translated to C++ for the parser, and to use a DSL for specifying the AST. This approach keeps the mistakes and tedium down because shell is an extremely large language syntactically. (It could be the biggest of any language except for perhaps Perl. Shell is relatively small semantically, but huge syntactically (which is why I'm preoccupied with parsing :) ).

Re:The naked truth about writing a programming language (2014) by matheusmoreira on Saturday May 02, 2020 @05:26PM (#23055055)
> A main point here is that you have to consider the lexer and parser separately. Grammars don't address this distinction, which arises in essentially all programming languages.

So why does this happen? Couldn't context-free language parsers emulate a lexer by treating individual characters as symbols?

Re:The naked truth about writing a programming language (2014) by chubot on Saturday May 02, 2020 @06:19PM (#23055347)
If there's no feedback, you can consider the lexer and parser separately as languages.

- The lexer recognizes a set of strings of characters.

- The parser recognizes a set of strings of tokens (as returned by the lexer)

But those two languages will have different power in general, so, like Jim points out, when you say something like "Python is context-free" or "Haskell context-free", it's not clear what you're talking about. And it's really false under any reasonable interpretation.

So you can consider them separately, and make a precise statement. But if there's feedback between the two, then you can't do that anymore. The theory doesn't tell you any properties a that the (lexer + parser + feedback mechanism) possess.

That is, regular languages and context-free languages have all sorts of properties proven about them, including ones that let you write code generators. You don't get any of those properties when you have ad hoc feedback mechanism between the lexer and parser.

----

Someone could come up with formalisms for specific types of feedback.

I think OCaml's Menhir has done some of this, but I don't remember the details off hand.

They have written parsers for C (CompCert) and POSIX shell and addressed some of the gaps between theory and practice. I don't use it but they're at least tackling the right problems.

But note that C uses a specific type of feedback (the lexer hack), which isn't identical to what other languages use. So you would have to come up with a formalism for each one, and probably nobody has done that.

Re:The naked truth about writing a programming language (2014) by zzo38computer on Saturday May 02, 2020 @06:02PM (#23055260)
Is Haskell context-free if you don't use the indentation layout mode? Haskell does support using braces and semicolons too. However, this is not true of Python (as far as I know).
Re:The naked truth about writing a programming language (2014) by massysett on Saturday May 02, 2020 @07:35PM (#23055726)
In theory Haskell supports braces and semicolons but in practice nobody uses them, leading to bugs like this one:

https://github.com/haskell/haddock/issues/12

Re:The naked truth about writing a programming language (2014) by zzo38computer on Saturday May 02, 2020 @08:16PM (#23055925)
I do use braces and semicolons when programming in Haskell, and I have encountered that bug before, and I hope that they will fix it (although it still seems to be unfixed after six years, but a few people clearly do care, even if others don't care).
Re:The naked truth about writing a programming language (2014) by tanilama on Saturday May 02, 2020 @07:10PM (#23055610)
what is context free language anyway? context free grammar had a clear definition in parsing, not sure i understand how that can be extended to languages.
Re:The naked truth about writing a programming language (2014) by chubot on Sunday May 03, 2020 @02:40AM (#23057312)
The OP is abusing the term "context free". He's saying it avoids "the lexer hack" [1]:

Context free grammars. What this really means is the code should be parseable without having to look things up in a symbol table

That's NOT what context free means. That's a narrow view from someone designing a C-like language and trying to avoid a very specific property of C.

Another example of being context-sensitive, which has nothing to do with symbol tables, is that resolving LALR(1) conflicts in a yacc-style grammar can make the language not context-free. To resolve ambiguity the parser is doing stuff "outside" the grammar.

----

"Context free" is a mathematical term with a precise definition. It's a class of languages that's part of the Chomsky hierarchy [2], which was discovered and described by linguists and mathematicians before any programming language existed, and has applications outside programming languages. Wikipedia does a good job:

https://en.wikipedia.org/wiki/Context-free_grammar

A formal grammar is considered "context free" when its production rules can be applied regardless of the context of a nonterminal. No matter which symbols surround it, the single nonterminal on the left hand side can always be replaced by the right hand side. This is what distinguishes it from a context-sensitive grammar.

Simple examples of languages that's are context-sensitive (not context-free): Lua string literals, Rust raw strings (see other comments in this thread), and shell here docs:

http://lua-users.org/wiki/StringsTutorial

Yes:

    [[ mystring ]]
    [=[ mystring ]=]
    [==[ mystring ]==]
No:

    [=[ mystring ]]    # mismatched
Matching this language requires a context-sensitive grammar and can't be done with a context-free grammar. It's not all that easy to prove: See the posts from Trevor Jim regarding proofs.

The C language is also not context-free.

[1] http://www.oilshell.org/blog/2017/12/15.html#appendix-lexing...

[2] https://en.wikipedia.org/wiki/Chomsky_hierarchy

Re:The naked truth about writing a programming language (2014) by bmn__ on Sunday May 03, 2020 @06:42AM (#23058182)
> [CF] was discovered and described by linguists and mathematicians before any programming language existed

Nitpick: that was 1956, by then a few PL did already exist.

Re:The naked truth about writing a programming language (2014) by wtetzner on Saturday May 02, 2020 @09:50PM (#23056264)
I read it to mean that the language's grammar is context free, but I guess I'm not sure if that's what was meant.
Re:The naked truth about writing a programming language (2014) by chrisseaton on Saturday May 02, 2020 @05:01PM (#23054872)
> Most languages have context-free syntax

Do they? I haven't done a survey of languages but I would guess most are context-sensitive.

Re:The naked truth about writing a programming language (2014) by bmn__ on Sunday May 03, 2020 @07:03AM (#23058268)
I would guess so, too. In my experience, they do strive to be CF, the CS parts express mildly/are few and far between and often are accidental.

That makes using a CFG parser practical and hacking around the problems caused by CS does not take much effort.

Re:The naked truth about writing a programming language (2014) by steveklabnik on Saturday May 02, 2020 @02:57PM (#23054023)
Rust is almost context free; there’s one relatively rarely used bit that requires context. And usually it’s only one or two bytes of it.
Re:The naked truth about writing a programming language (2014) by h-cobordism on Saturday May 02, 2020 @03:50PM (#23054386)
You're referring to raw string literals, right?
Re:The naked truth about writing a programming language (2014) by steveklabnik on Saturday May 02, 2020 @04:59PM (#23054861)
Yes. You need to include more #s than you have in the string, and it’s rare to have a ton of them, let alone a raw string literal in the first place.
Re:The naked truth about writing a programming language (2014) by wtetzner on Saturday May 02, 2020 @09:51PM (#23056268)
Does that make the grammar context-sensitive, or just the lexer?
Re:The naked truth about writing a programming language (2014) by steveklabnik on Sunday May 03, 2020 @09:18AM (#23058812)
This is not an area of my expertise, so I can't say.
Re:The naked truth about writing a programming language (2014) by tom_mellior on Saturday May 02, 2020 @05:28PM (#23055067)
This point in the article is pretty weird (the rest is good). I don't think IDEs do a lot of parsing to do syntax highlighting, isn't it all just regex matching to identify the types of tokens? I'd be interested in real-world examples of IDEs doing something more complex to achieve syntax highlighting. And conversely, examples of imperfect syntax highlighting of C++ due to the undecidability of its input language. I mean, yes, an actual parser for C++ must be able to execute template computations, and that's a pain, but why would that be relevant to syntax highlighting? It isn't.

Now, there are valid reasons for running a proper language frontend from the IDE: Error reporting of all kinds, not just syntax but also type errors and whatever else the compiler likes to complain about. But there the IDE should not try to replicate the parser. Parsing only a context-free surface language will not catch type errors, exactly because enforcing a static type system requires one to "look things up in a symbol table". So the actual compiler should provide an IDE-friendly mode where it runs its frontend and reports errors back, and the IDE should not try to roll its own version of this.

In other words, the IDE is not relevant either way.

> Any programming language that is massively adopted is context free?

Statically typed languages require semantic analysis and symbol table lookups, so they are out.

And yet we have context-free grammars for all widely adopted statically typed programming languages. And we also have additional semantic checks for all of them. The notion of a "context free programming language" being one that has context free syntax and no semantic checks at all is not a useful notion. Don't worry about it while designing your language.

Re:The naked truth about writing a programming language (2014) by geophile on Saturday May 02, 2020 @06:28PM (#23055400)
I'm pretty sure that the JetBrains products (Intellij, PyCharm, etc.) have a pretty deep understanding of the languages they handle. Java is strongly typed, and Intellij does a perfect job, (as far as I can tell), of highlighting, and more importantly, refactoring. Hard to see how they could do that with just regex matching.

By contrast, Python is more dynamic, and so the refactoring in PyCharm is pretty weak. It usually catches some of what it needs, but I often need to find the rest. It does appear to be the case that PyCharm is doing flow analysis to infer types. I.e., even PyCharm is doing more than regex matching.

Re:The naked truth about writing a programming language (2014) by tom_mellior on Sunday May 03, 2020 @07:55AM (#23058475)
> highlighting, and more importantly, refactoring. Hard to see how they could do that with just regex matching.

The former is easy, the latter is impossible with regexes only. Which tells you that they are very different things. For refactoring you need not only a proper parse, but also the ability to "look things up in symbol tables". Which is what I said: There are good reasons to run a proper language frontend from the IDE. But syntax highlighting isn't one of these reasons. And refactoring isn't possible with only a simple parse without using symbol tables.

Re:The naked truth about writing a programming language (2014) by pasquinelli on Saturday May 02, 2020 @05:42PM (#23055161)
>> Any programming language that is massively adopted is context free?

> Statically typed languages require semantic analysis and symbol table lookups, so they are out.

Isn't that the typechecker, not the parser?

Re:The naked truth about writing a programming language (2014) by tom_mellior on Sunday May 03, 2020 @08:04AM (#23058501)
Depends on what level you're thinking of: "Pure" grammar or "well-formed programs acceptable to the compiler". C++ does have a context-free grammar (it's in the standard), and also a lot of additional constraints on what constitutes a well-formed programs (this includes type checking). Since we're discussing a claim that "C++ is famously not a context free grammar", we must be thinking of the latter notion and include the type system in our considerations. In which case, we must consider the entire frontend as a whole.
Re:The naked truth about writing a programming language (2014) by johnmaguire2013 on Friday May 08, 2020 @09:43PM (#23121433)
Many syntax highlighters actually interpret the language to some extent. For example, Atom: https://github.blog/2018-10-31-atoms-new-parsing-system/

Anyway I'm just posting now as I first read this comment when you posted it and tried to recall this blog post by the infamous Steve Yegge, and just finally did: http://steve-yegge.blogspot.com/2007/06/rich-programmer-food...

Re:The naked truth about writing a programming language (2014) by neutronicus on Saturday May 02, 2020 @06:03PM (#23055279)
Emacs definitely does parsing of C/C++ (or delegates to an LSP server, depending) in order to do indentation, users' preferences for which tend to depend heavily on syntactic context.
Re:The naked truth about writing a programming language (2014) by tom_mellior on Sunday May 03, 2020 @07:58AM (#23058486)
Indentation is a good point, you need a proper parse for that. But for that you don't need the ability to consult symbol tables, or resolve nasty C++ template metaprogramming stuff. A context-free grammar suffices, and C++ has that. C++ also has some very complicated (undecidable) rules for what programs are well-formed and should be accepted by the compiler, but the editor's indentation tool doesn't need to worry about those.
Re:The naked truth about writing a programming language (2014) by alpaca128 on Saturday May 02, 2020 @05:37PM (#23055136)
> I don't think IDEs do a lot of parsing to do syntax highlighting, isn't it all just regex matching to identify the types of tokens?

IntelliJ highlights variables etc. differently if they're unused and can highlight different identifiers in different colors. Not sure about other current IDEs.

That said I never felt like I needed such features, normal syntax highlighting does the job pretty well for me.

Re:The naked truth about writing a programming language (2014) by dehrmann on Saturday May 02, 2020 @06:59PM (#23055554)
IntelliJ has context-aware autocomplete. Type this.field.get and it will show methods and fields starting with get for whatever type field is. There's a lot of context needed to make that happen.
Re:The naked truth about writing a programming language (2014) by harry8 on Saturday May 02, 2020 @10:35PM (#23056456)
Eclipse has refactoring told for at least Java and c{,++}

Variable renaming respecting scope, function renaming that updates the name at the call sights but ignoring overloads, function extraction... and so on. You may not use it or indeed use eclipse at all.

I can't tell you about the details, clearly something going on beyond syntax highlighting via keyword regex matching. With a beefy enough box it all works well enough.

Re:The naked truth about writing a programming language (2014) by tomp on Saturday May 02, 2020 @05:58PM (#23055246)
The purpose of context-free, or rather unambiguous grammars in general, isn't for the benefit of compilers, but for the benefit of humans. A computer can parse almost everything, even regexes (with backtracking etc.), it's the humans that trip over fancy grammars.
Re:The naked truth about writing a programming language (2014) by froh on Sunday May 03, 2020 @03:07AM (#23057389)
Notwithstanding the other comments on context free vs sensitive, the 2014 article predates the language server protocol, which neatly moves the support for programming languages from the IDE to a stand alone tool tied to the language: https://microsoft.github.io/language-server-protocol/

https://en.m.wikipedia.org/wiki/Language_Server_Protocol

So that IDE support point has become much weaker, if not moot.