Maybe I miss something, but do the so called markup languages have the equivalent concept of semantics that you can find in C or C++?

Judging from how you parse the language, you don't really have too many options, there isn't too much context, you just parse your tokens and apply them to the source code.

I'm tempted to say that not only semantics doesn't exist in this case, but this only happens in markup languages: am I right?

  • 1
    What makes you think they don't have semantics? Can you give an example? – Doval May 11 '14 at 16:33
  • @Doval for example the html or markdown, <p>text text</p> there is no context here – user2485710 May 11 '14 at 16:35
  • 3
    The semantics is a parragraph of text. If a browser (or more generally any user agent, like a screen reader for the visually impaired) presents that text in a non-continuous manner, e.g. as a list of individual elements, it is most certainly dead wrong. – Doval May 11 '14 at 16:37
  • @Doval what this has to do with the language or the parser ? – user2485710 May 11 '14 at 16:38
  • 4
    What does the parser have to do with semantics? Are you sure you're not thinking of syntax? – Doval May 11 '14 at 16:50

Common markup languages and common programming languages are indistinguishable in that they have both syntax and semantics. They differ only in that the purpose of a programming language is to generate executable code for a processor, while the purpose of a markup language is to generate declarative data for a display engine.

Both have a lexical structure. They are made up of tokens like words and numbers and punctuation.

Both have a syntactical structure. The tokens can be arranged in many different ways, and some arrangements are valid while others are not.

Both have a semantic structure. The tokens have meaning, some being predefined and some user-defined, and the way that tokens are arranged has meaning.

If we take the following code fragment:

<div class="yolo">Hello world</div>

The tokens div, class and the angle brackets have pre-defined meanings. The token "yolo" is a string which provides a user-defined name for the div. The angle brackets separate the markup from the text. These are semantic observations.

For both, the parser phase of the compiler stops at the same point, when it has generated a suitable internal representation (such as an AST).

The next phase for a programming language is to generate code. The next phase for a markup language could be to generate a DOM. That phase is dependent on the the semantics, but not part of it.

  • same question for you programmers.stackexchange.com/questions/238735/… – user2485710 May 13 '14 at 7:58
  • You can find an explanation of context-free grammars in any text book or web search. Feel free to ask a new question. The comments are not a forum. – david.pfx May 13 '14 at 15:22
  • well, personally I always think that even the best question can't trigger an optimal response and some related details are likely to left out in the first take. If you could name a couple of good books about that that would be nice so I will get the basic know to understand how a language works and the differences between 2 or more given languages. – user2485710 May 13 '14 at 16:07
  • @jmoreno: No, I would say that while individual languages differ, the groups rely on syntax and semantics in exactly the same way. A single definition for semantics fits both. – david.pfx May 13 '14 at 22:53
  • @jmoreno: I don't see what your point (if any) has to do with the question. Obviously languages are different and serve a different final purpose, but there is nothing obvious about the construction of a language or the way it expresses its semantics that forces it into the markup or programming category. Tex and HTML look quite different, XML and PHP look quite similar, their purposes are wildly different. So what? – david.pfx May 14 '14 at 23:11

A language is defined (a) by its syntax which includes the grammar and the lexical structure or vocabulary, and (b) the semantics, which is the meaning. In C, the snippet a + 1 has the meaning “add the contents of variable a and the integer 1”. In HTML, the snippet <p>foo bar</p> has the meaning “a paragraph element containing the text foo bar”.

So markup languages do have semantics (i.e. a meaning). However, semantics have nothing to do with parsing. During parsing, the input is broken up according to the lexical structure. These tokens are then arranged according to the grammar of the language. E.g. the English snippet you asked a question might be arranged as

subject: 'you',
predicate: (verb: 'ask',
            tense: past,
            object: (article: indefinite,
                     noun: 'question'))

This structure helps figuring out the meaning, but it's not the same as the actual meaning.

There can also be well-formed sentences that do not have valid semantics. For example, the above C example a + 1 makes no sense if a holds a struct instead of a numeric type. In English, Colorless green ideas sleep furiously would be an example of a well-formed, but nonsensical sentence.

  • it's the lexer that breaks down the input using the lexical structure, the parser is the next step, also your explanation doesn't take scannerless parsing and context-free languages into account. I can't see how this explanation could be correct, for example in C++ usually the token that represents the type triggers a given semantics in the parser, but the token/lexer alone is not able to do anything that involves semantics because for the lexer there are no types at all, just tokens. – user2485710 May 11 '14 at 23:27
  • 6
    @user2485710 For the purpose of discussing semantics, lexers and parsers are irrelevant. They're just there to figure out the syntactic structure of the input. Once a well-formed piece of input has been parsed, meaning is assigned to the input. E.g. the HTML standard defines semantics for each tag type. If a browser renders the contents of <b> as bold, that's an execution of these semantics. The whole point of a markup language is to label content with certain semantics: <p> means a paragraph, <em> means an emphasis, <a href="…"> means a link, … – amon May 12 '14 at 0:14
  • 2
    @user2485710 Don't mistake "context-free" for "without meaning". In language analysis, "context-free" means that the language can be described by a regular, structured grammar. It does not mean that the language has no semantics. – Ross Patterson May 13 '14 at 11:07
  • 1
    @user2485710 grammars/languages can be classified by how complicated they are syntactically. Regular Languages are the most simple, and can be parsed by a simple state machine. Number formats such as 123.40E2 are an example. Context-Free Languages are more complicated, but they can be described by simple rules. Nested braces (()[{}]) are an example of such a language. Most programming languages use a pseudo-context-free grammar. Parsing rules in Context Sensitive Languages have to take surrounding rules into account. In English the indefinite article a/an is context sensitive. – amon May 13 '14 at 12:48
  • 2
    @user2485710 Required reading: The Chomsky Hierarchy, which explains different classes of grammars. Follow the links if you're unclear about a term. – amon May 13 '14 at 13:07

Markup languages most definitely have syntax (specific sequences of characters that defines tokens) and semantics (meaning to be applied to parsed content). You can't really have a language without both. You might possibly be able to argue about XML (semantics are really specific to a particular format, making each a DSL), but you can’t possibly argue about HTML.

What HTML lacks is EXPRESSION STATEMENTS or variables. Without adding in CSS there's no way to make it do a calculation by itself. This means it's not able to perform any calculations and is limited to giving semantics to content -- it can neither create or modify content on it's own.

Note that the way you typically add/modify html content on the fly is only possible because html has semantics -- in particular, you can say: this bunch of text is a computer program in language x, run it (where X is generally javascript).

  • Could you make an example about a language with expression statement and another one without them. – user2485710 May 13 '14 at 7:59
  • @user2485710: html is a language without expressions and/or statements, and any programming language will have one or the other and generally both. Lisp is a language without statements, brainfuck is a language without expressions. The ability to do a calculation is what makes a programming language. That is what HTML lacks, and why although it is a language, it is not a PROGRAMMING language. – jmoreno May 13 '14 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.