1

So if I understand correctly, in C++ we have a bit-shift operator:

int a = 1024; a = a >> 1; 

And we can also have two > tokens in a row in some contexts, i.e. in a template:

Foo<Bar<int>> x = ... 

How do C++ parsers differentiate between those cases? Do C++ lexers need to have context information when they're parsing tokens to know whether they're parsing a pair of angle braces or something?

8
  • 2
    Well, the c++ compilers weren't guaranteed to distinguish these before th c++11 standards. In case of templates you had to add a whitespace character between the closing angle brackets. Commented Mar 27, 2021 at 16:16
  • Related: stackoverflow.com/questions/29331315/… Commented Mar 27, 2021 at 16:17
  • Short answer: yes, lexers use context information. Things get rather dicy when >> is truly ambiguous. Commented Mar 27, 2021 at 16:20
  • Unfortunate C++ syntax choice for templates, but way too late to fix it now. I wish C++ would have used a template syntax like, say, Foo!(Bar!int) but that ship has long since sailed. Commented Mar 27, 2021 at 16:37
  • @sam: >> is never ambiguous. The problem is that it's not always easy to know whether a < is a less-than operator or the start of a template argument list. Although that determination requires resolving the name which precedes the <, a task which goes far beyond what lexers are generally responsible for, the answer is always clear. And once that determination is made, the decision about >> is very simple -- if a > can close a template argument list, then it does so. See this brief answer. Commented Mar 28, 2021 at 4:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.